Example 2: Mixing char and int data types
Strings are read byte by byte, character by character
Integers are read as a word - in this case, in a group of 4 bytes
Name: Jim Smith (string data, read byte by byte)
Age: 21 (integer data, read word by word - in groups of 4 bytes)
Dept: 260 (integer data, read word by word - in groups of 4 bytes)
Big Endian:
0 1 2 3
0 J I M __
4 S M I T
8 H 0 0 0
12 0 0 0 21
16 0 0 1 4
Little Endian:
3 2 1 0
__ M I J 0
T I M S 4
0 0 0 H 8
0 0 0 21 12
0 0 1 4 16
In both Big Endian and Little Endian, the string data is read byte by byte
starting with numerically lower byte. Both "JIM"'s are read starting with byte
0 to byte 3. Big Endian reads strings left to right, Little Endian reads strings right to left.
Integer data - read all 4 bytes at one time:
If the bytes are transferred byte by byte from Big-->Little, the string data
comes out okay, but the integer data comes out reversed:
Little Endian:
3 2 1 0
__ M I J 0
T I M S 4
0 0 0 H 8
21 0 0 0 12
4 1 0 0 16
21 0 0 0, which is interpreted as
21*256^3 = 21*2^24 in Little Endian
Both systems look at the integer as one complete word, 4 bytes
If you try reversing each word after it's been transferred byte by byte,
the integer comes out okay, but the string data is not right:
J I M __ --> (byte by byte) __ M I J
--> (then reverse) --> J I M __
which is okay for Big Endian, but is read as " MIJ" in Little Endian.