in reply to Re: A little C code converted to Perl *HELP*
in thread A little C code converted to Perl *HELP*

yep i think you are right, would you mind give me this little perl code which will make a random gcodeid like the c script do... you would do me a world-big-favour ;))
  • Comment on Re: Re: A little C code converted to Perl *HELP*

Replies are listed 'Best First'.
Re: A little C code converted to Perl *HELP*
by jonadab (Parson) on Nov 03, 2003 at 04:42 UTC

    Oh, I almost missed that little part at the end with the sprintf...

    DWORD generated_check=(rand()<<16) | rand(); char gcheck[64]; sprintf(gcheck, "%x", generated_check);

    My C, as I said, is marginal, but if this is doing what I think it's doing, ...

    $gcheck = printf "%x", ((rand() << 16) | rand());

    I'm assuming here that the C << operator is a left shift just like in Perl, but that's a wild guess. I'm also assuming that rand with no args in C does the same thing as in Perl, which is also a guess. Like I said, my knowledge of C leaves something to be desired. I know what the Perl code does. (It prints "0" every time.) I'm guessing at the C code. Probably guessing wrong.


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

      The rand function in C returns a random integer. The OP's C code seems to assume 16 bit integers, so that's a max of 65535 (or 32767 to -32768, IIRC). So, I think the equivalent Perl code would be something like:

      $gcheck = sprintf "%x", ((int(rand(65536)) << 16) | int(rand(65536)));

      bbfu
      Black flowers blossom
      Fearless on my breath

      okay, great many thanks mate!!

      (if someone thinks this wasnt correct, pls reply another solution)

      Thanks again mate!!! :))
      hmmm cant get that line working you gave me .. always give me 0 , or 1 ...

      i think somethings wrong there :( hmmmm

      any solution code? many thx
        cant get that line working you gave me .. always give me 0 , or 1 ...

        Yes, of course. Should almost always be 0, I think. (rand with no arguments produces a number between 0 and 1; you're left-shifting that a whole bunch and then evaluating it in integer context.) The question is, what did you *want* it to do? If you can tell me what the C code *does*, we can save all this guesswork and get straight to a _working_ example. Even a really *vague* explanation in English would be preferable to C code. I tried reading the C code, but I had to make some assumptions about things I wasn't sure about, and since you're unhappy with my translation, it's apparent that some of my assumptions were wrong. In particular, I suspect I was wrong about what rand with no args does in C. If I needed to know that sort of thing, I'd be hanging out in comp.lang.c or someplace (or else committing ritual suicide).


        $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/