During the first weeks of my stay in my company, my boss has been feeding me with lots of information. Giving me lectures on different kind of stuff in programming, computing, database systems, etc. It all started when I asked him about how to invert a string without using arrays. He gave me an algorithm on how to solve it and along the way, one of the things he mentioned was about the “ENDIANNESS” of a system. He explained lots of things, but it really didn’t sink in to me because i barely understood what he was trying to say.
A few weeks later, after being given some task to create an application, I encountered a problem with the encoding of the data. So he showed me a hex dump of the data and separated it by character. There was indeed a weird character. It displays a space character in the data but after dumping, it’s not the same as ASCII 32.
Going back to what he had explained (2 months have passed I think), I remembered him telling me that there are two kinds of endianess, the big endian and the little endian. the normal PCs sold in the market (like intel’s x86) follow the little endian. The MAC book (or powerpc) architecture follow the big endian. Now what does this actually mean? I won’t go into details, but i’ll just be explaining the idea behind it.
Big endian means that the bytes are read from left to right. This means that the highest address or most significant byte in the memory is found on the right.
For example, in the given indexed array
Example 1:
| n | … | R | I | D | V | A | N | … | n |
—————->>> most significant byte
The big endian reads the data from left to right, therefore D has a higher address compared to R.
Little endian on the other hand is the opposite. The bytes are read from right to left so the highest address is in the left most side of the memory.
For example, in the given indexed array
Example 2:
| n | … | R | I | D | V | A | N | … | n |
most significant byte <<<—————-
In this case, R now has the higher address compared to D.
So what happens when two different architectures will communicate? Sometimes, the bytes gets inverted. Because if the data is from a big endian then transmitted to a little endian, it will read the data in the reverse order hence inverting the data in the memory. Example, a data from a big endian machine is being sent to a little endian. the array would now look somewhat like this.
example 3:
| n | … | N | A | V | D | I | R | … | n |
—————->>> most significant byte
The example looks confusing right? Just try to reverse example 2 and it would look like example 3.
So when you’re trying to exchange data from two different architectures, always bear in mind that data loss might happen. 🙂