Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Reading (and parsing) a byte stream

by acid06 (Friar)
on Mar 05, 2006 at 03:17 UTC ( [id://534553]=note: print w/replies, xml ) Need Help??


in reply to Reading (and parsing) a byte stream

I'm shooting in the dark here, but...

I have a hunch ord($byte) is what i'm looking for, but the byte i'm reading isn't yielding the value I expect it to, which could be a different problem...

...sounds to me like a problem related to the "endianness" of the data.

Instead of using ord(), try using some form of unpack(). The protocol specification should specify the endianness (i.e. if it's big- or little-endian).


acid06
perl -e "print pack('h*', 16369646), scalar reverse $="
  • Comment on Re: Reading (and parsing) a byte stream

Replies are listed 'Best First'.
Re^2: Reading (and parsing) a byte stream
by ChemBoy (Priest) on Mar 06, 2006 at 03:57 UTC

    I'm afraid your shot misses—endianness has to do with byte order, not the significance of bits within bytes. So if you have a four-byte number, the most-significant byte may be at the beginning (Big-endian, or $Config{byteorder} eq '4321') or at the end (Little-endian, '1234'), but the values of the bytes themselves don't change.



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

      Not really.
      From the article about Endianness at the Wikipedia:

      Endianness also applies in the numbering of bits within a byte or word. In a consistently big-endian architecture the bits in the word are numbered from the left, bit zero being the most significant bit and bit 7 being the least significant bit in a byte.

      So endianness should have to do with bit order.
      Just because the usual way of packing/unpacking little- or big-endian data in Perl (Network and VAX types) does not follow this pattern it doesn't mean it's not correct.


      acid06
      perl -e "print pack('h*', 16369646), scalar reverse $="

        Bit order only matters if you've got a way of addressing things smaller than bytes or if you've encoded bits from one byte into multiple bytes. That isn't the case here so bit order should not matter. The data is transmitted in units of bytes and thus the values of the bytes are preserved, no matter how each unit along the way chooses to store those byte values.

        - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://534553]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-16 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found