Aug 232011
 

It is one of the problems some network programmers meet while coding. While debugging executable files You may have noticed that, when seen in hexadecimal editor, some data in the files is ordered backwards. First you see the 1 and then several zeroes. But you know for sure, that the value you encoded for a constant is actually “1” instead of 01 00 00 00.This is what Little endian byte order actually is.Have a look in the decimal value 305,419,896 in HEX. It is 0x12345678. If your machine is Big endian, you will see this value stored in a binary file looking the same way it is. But if your machine is little endian, The value stored in the file will look like 78 56 34 12. The least significant byte will be in front, and the most significant will be in back.

The reason for this goes back in the computer’s past, when it was more important to make MATH really fast in spite of easiness. Imagine you have 1 physical address. This address has the value 01 00 00 00. If you need to read this address as byte, word or long integer. You will ALWAYS get the value 1, which is the real value. If you are using Big endian byte order, you will need to get the address + 4 if you need byte, address + 2 if you need word. The little endian byte order is still used in network equipment and serial interface dialog, so it’s good to know how to cope with it… or you will get really annoyed 😉

01 00 00 00 (byte) = 1
01 00 00 00 (word) = 1
01 00 00 00 (long) = 1

Also, there is the need for speed. If your processor has only 3 real registers (X, Y and Accumulator), than it’s easier to compute the values if they are little endian.

Y = 0;
A = 1;
foreach byte do {
    X = readbyte;               # we read next byte
    if X > 0 do {               # Check if the computation is needed at all.
        Y = Y + (X * A);        # we use Y to collect the results of all bytes multiplied by A
    }
    SHL A << 8;                 # A grows from 1 to 256 to 65536 to 16777216 (2^0, 2^8, 2^16, 2^24)
}

This is of course an ideal case of what is now known as 64 bit processor. One that can use 64 bit words and registers. In case you are using Big endian byte order you cannot use the multiplication but integer division, which is slower.

If you are Linux user but unsure what endianness your equipment uses, execute this:

bash-4.1# perl -MConfig -e 'print "$Config{byteorder}\n";'

If you want this byte order reversed and shown using simple math, use this Perl script:

#!/usr/bin/perl -w

my $a1;
my $a2;
my $a3;
my $a4;
my $r;

if (! $ARGV[0]) {die "There are no args";}

$r = $ARGV[0];

$a1 = $r % 256;
$a2 = ($r - $a1) % 65536;
$a3 = ($r - $a1 - $a2) % 16777216;
$a4 = ($r - $a1 - $a2 - $a3);

$a2 = $a2 / 256;
$a3 = $a3 / 65536;
$a4 = $a4 / 16777216;

printf ("\n$r is equal to 0x%02x 0x%02x 0x%02x 0x%02x\n", $a4, $a3, $a2, $a1);
printf ("$r is reverted to 0x%02x 0x%02x 0x%02x 0x%02x\n\n", $a1, $a2, $a3, $a4);

This is just a rough example. There is easier way to make and explain it. Get the input value, the least significant byte can be obtained by getting the "modulo" of integer division from the input value, store this modulo in an array. After this subtract the modulo from this input value and divide the value by 256. This is the new input value. Repeat, until value is zero. Voila. You have an array of [most significant, less significant ... least significant] bytes.

The script will only work on Long integers, but can easily be made to work with 64 bit words. Made It short, because it's more easy to read. With Little endian, we will always see backward byte order:

bash-4.1# ./RevertByteOrder.pl 305419896

305419896 is equal to    0x12 0x34 0x56 0x78
305419896 is reverted to 0x78 0x56 0x34 0x12

bash-4.1# ./RevertByteOrder.pl 22136

22136 is equal to    0x00 0x00 0x56 0x78
22136 is reverted to 0x78 0x56 0x00 0x00

bash-4.1# ./RevertByteOrder.pl 4660

4660 is equal to    0x00 0x00 0x12 0x34
4660 is reverted to 0x34 0x12 0x00 0x00

bash-4.1#

And if you need to see how many bytes per integer and long integer is your equipment using, execute this:

bash-4.1# perl -V:{short,int,long{,long}}

I hope this explains the Little and Big endian byte order. You can check the articles I've used for more information.

* http://stackoverflow.com/questions/2610849/finding-if-the-system-is-little-endian-or-big-endian-with-perl

* http://docstore.mik.ua/orelly/perl/prog3/ch25_02.htm

 Posted by at 12:07 pm

Sorry, the comment form is closed at this time.