Understanding Big and Little Endian Byte Order

hi,
actually i know nothing about the c-language, but i have a question.
i have a riddle. it is: 71+, OÄE
i have to get numbers of this riddle, that is, i have to “translate” this riddle somehow into numbers.
and i know, that it has something to do with the c or c++ language and also something with intel byte order…
can you please help me? i would be most thankful.

Hi Joonas, I’m not exactly sure of the question – you have raw data and need to extract a value?

If you have 4 bytes (for example), I would examine it as a 4-byte integer, 4-byte unsigned integer, 2 shorts, and 4 characters (all in the big and little endian varieties). I’d then try to look for some pattern in the results.

Very nice article! crisp and to the point…

Thanks Hema, glad you liked it.

Ahh… Finally i got it.
What an explanation!
Concepts, Simplicity, Analogies, Humor and Writing style - first Question then Answer.
Really, Thankx A Lot.

Awesome Apurva, glad you enjoyed it!

Do you have any idea what machine uses which endian? For example, how about PC, big endian or little endian? Unix, big endian or little endia?

excellent job. the way you explained it is the way things should be explained in this world.

Simple and interesting presentation

Thanks for an excellent explanation. Just one question: you only mention integers - what about floating point values? How are they converted between endianness?

@Shaoji: Good question, there is a partial list here: http://en.wikipedia.org/wiki/Endianness#Endianness_and_hardware. The endian issue is more with the processor (x86 Pentium) than the OS (Unix).

@Barn: Thanks, glad you liked it.

@Basheer: Thanks!

@Paul: Appreciate the kind words. Great question – I believe you would treat a floating point number as a 4-byte data type (on a 32 bit machine). Between different endian machines you’d have to reverse the bytes before reading the float (not positive but that’s my guess – data is data).

Here’s sample code for floating point / byte array conversion. We’ll start with a number (3.14159) and flip the sign bit (more info on floating point).

#include

int main(int argc, char** argv){
char *a;
float f = 3.14159; // number to start with

a = (char *)&f; // point a to f’s location

// print float & byte array as hex
printf(“float: %f\n”, f);
printf(“byte array: %hhX:%hhX:%hhX:%hhX\n”,
a[0], a[1], a[2], a[3]);

// toggle the sign of f – using the byte array
a[3] = ((unsigned int)a[3]) ^ 128;

//print the numbers again
printf(“float: %f\n”, f);
printf(“byte array: %hhX:%hhX:%hhX:%hhX\n”,
a[0], a[1], a[2], a[3]);

return 0;
}

And it outputs this on my machine:

float: 3.141590 byte array: D0:F:49:40 float: -3.141590 byte array: D0:F:49:C0

As you can see, we can access the floating point number as a byte array and change individual portions of it. This machine is little-endian. Notice how a[3], the highest byte containing the sign bit, is last. That means the little end, a[0], comes first.

Theoretically, on a big-endian machine the order of bytes would be reversed, but some sources say IEEE 754 floats are always stored little-endian. I don’t have a big-endian machine to confirm this :).

Keep posting…Very good article.

How will following structure look in big-endian and little-endian systems?
struct STRUCT {
unsigned int a:12;
unsigned int b:10;
unsigned int c:10;
};
struct STRUCT myStruct= {0xFED, 0×345, 0×3AB};

Why are sequence of characters not converted into network byte order when sending them to a remote UNIX server?
Example : - ‘Article’ is sent without converting to ‘elcitrA’.

Interesting Presentation!!
Just one question… Whether the byte order conversion for a 8-byte variable is similar to a 4-byte variable?

Excellent Boss!!! thnaks a billon…for presenting such a complex thing in a very lucid manner.

@anil: I’m not certain how structs are laid out on big/little endian systems (in the order variables are declared or otherwise). The easiest method may be to use the byte-walking method above.

@Sharada: Most network protocols should convert everything to “network order” when transmitting – it’s probably a bug if this doesn’t happen! (Or if the client & server agreed not to use the standard protocols).

@Ajish: Yes, I imagine 8-byte information is stored similarly (as the jump from 2 to 4, extrapolate from 4 to 8; it always pays to double check though!).

@pramod: Thanks, glad you enjoyed it.

Good article. I am having trouble talking to SD cards which are little-endian. I assume that, when sending the data serially, the LSB of byte 0 goes first.

Thanks Delron. When reading a stream of data for something as modern as an SD card, I’d assume that it would be given to you in 1-byte chunks, so hopefully individual bits aren’t an issue. (I.e. you could read/write 1 byte at a time – just my guess).