in reply to Re: Re: writing binary data in perl
in thread writing binary data in perl


I think the chr(1234>>8) is superfluous.

--
John.

  • Comment on Re: Re: Re: writing binary data in perl

Replies are listed 'Best First'.
Re: Re: Re: Re: writing binary data in perl
by sfink (Deacon) on Apr 12, 2002 at 21:14 UTC
    What do you mean by "superfluous"? It's the closest analogue to writing out bytes from C, assuming you're on a little-endian machine.

    But the question was ambiguous about the desired outcome. Writing out the 'int' value 1234 might mean (1) writing either a little- or big-endian 0x4d2, (2) writing a host- or network-order 0x4d2, or (3) writing out the unicode codepoint 1234. I picked interpretation #1. #2 should be done with pack(), but since the sample C code didn't include htonl() or a variant, it apparently wasn't desired. #3 should be done with either pack() or a plain chr(1234).

    Except I'm still wrong, because I was really treating the 'int' as a 'short' and writing out only 16 bits. To match the C code, which wrote a 32-bit value, that should be chr(1234 & 0xff).chr(1234>>8).chr(0).chr(0) The distinction seemed unimportant for the simple goal of mentioning chr()!


      My apologies, you are correct. I misread your code and your intention.

      --
      John.