• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

why are bytes often reversed order when "packing" data?

Red Squirrel

No Lifer
I see this in protocols and other situations, where data is packed in a way so no matter what the value of a number is, it occupies the same space, for easy reading.

ex:

Say a protocol starts with a header saying the packet size, it will send something like:

0x0 0x0 0x3

The receiver knows to read 3 bytes, then turn that value into an int, and that's the size. In this case 3.

0x0 0x1 0x4

would be 20, for example


now I've also seen some do this:

0x4 0x1 0x0.

Basically, they reverse the bytes, so when you read it, you have to oreorder them before you can get the value. This slows down the program when this has to be done very often. Is there any given reason some protocols do this?
 
Originally posted by: DaveSimmons
Yep, the endians.

Some formats were first designed on non-x86 processors, so they had the endianess reversed.
(emphasis added in the above quote)
Thats discrimination! There is no 'correct' endianness! 🙂

 
Originally posted by: degibson
Originally posted by: DaveSimmons
Yep, the endians.

Some formats were first designed on non-x86 processors, so they had the endianess reversed.
(emphasis added in the above quote)
Thats discrimination! There is no 'correct' endianness! 🙂

All bytes are created equal!
 
Originally posted by: degibson
Originally posted by: DaveSimmons
Yep, the endians.

Some formats were first designed on non-x86 processors, so they had the endianess reversed.
(emphasis added in the above quote)
Thats discrimination! There is no 'correct' endianness! 🙂

Haha, as soon as I read Dave's message I knew that two or three posts down the page some sad, misguided person would suggest that big endian-ness is somehow less than perverted. Just remember, this isn't a matter of personal preference. No, it's war.
 
Network Byte Order is an established convention in datagram exchange. If your processor isn't in the same byte order then you too will need to reverse the bytes prior to working with them on your platform.

Most languages/platforms have a very fast routine for doing this. ntoh/hton routines in C/C++ and IPAddress.NetworkToHost() are just such examples. No reason to do it yourself. Just get used to always doing it after you select() from the socket or whatever you're doing.
 
Ah I see so it's more of a legacy thing I guess, where processors read backwards? Most of the time what I do to circumvent this is just read 1 byte at a time and I push it at the start of my arraylist rather then at the end, so it re-orders properly. I coded a special class to deal with data at the bit level so I can grab a bunch of bits and convert to int, and such. Very handy for manipulating serialized data.
 
Originally posted by: RedSquirrel
Ah I see so it's more of a legacy thing I guess, where processors read backwards?
It is YOUR processor that reads backwards! Down with your 0x0c0d0a0b, long live my 0x0a0b0c0d!!

Edit: Again, I screwed up the quotes...
 
Back
Top