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

Red Squirrel

No Lifer
May 24, 2003
70,616
13,817
126
www.anyf.ca
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?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Yep, the endians.

Some formats were first designed on non-x86 processors, so they had the endianess reversed.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
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! :)

 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
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!
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
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.
 

JasonCoder

Golden Member
Feb 23, 2005
1,893
1
81
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.
 

Red Squirrel

No Lifer
May 24, 2003
70,616
13,817
126
www.anyf.ca
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.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
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...