O
Oliver Knoll
Ok,
I've searched this group for Big/Little endian issues, don't kill me,
I know endianess issues have been discussed a 1000 times. But my
question is a bit different:
I've seen the follwing function several times, it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):
/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}
I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine? I've tried to understand
it with diagrams and everything, but my brain went just crazy! Can
anyone give me a simple explanation please?
I'm really curious...
And how would a function look like which does the "opposite": swap
bytes on a BE machine, don't change values on a LE machine (that is,
"read and convert LE data")?
Hope I can answer the 2nd quesion
myself when I understand the 1st question...
Here's the complete test program:
#include <stdlib.h>
#include <stdio.h>
/* This function is useful when reading and converting data which is
stored
in Big Endian Format.
- this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}
int main (int argc, char **argv)
{
int a;
char c;
short data1;
short data2;
char *d;
(void)argc;
(void)argv;
a = 0x01020304;
c = ((char *)&a)[0];
if (c == 1)
{
fprintf (stdout, "Integer a: %x - first byte: %x (MSB) -> Big
endian machine.\n", a, c);
}
else if (c == 4)
{
fprintf (stdout, "Integer a: %x - first byte: %x (LSB) -> Little
endian machine.\n", a, c);
}
else
{
fprintf (stdout, "Integer a: %x - first byte: %x -> A weirdo
machine.\n", a, c);
}
data1 = 0x0102;
d = ((char *)&data1);
fprintf (stdout, "Data[0]: %x - Data[1]: %x\n",
*d, *(d + 1));
data2 = getShortBE ((char *)&data1);
d = ((char *)&data2);
fprintf (stdout, "After GET: Data[0]: %x - Data[1]: %x\n",
*d, *(d + 1));
return 1;
}
Output:
-------
Data[0]: 1 - Data[1]: 2
After GET: Data[0]: 1 - Data[1]: 2
On Linux (i386):
Integer a: 1020304 - first byte: 4 (LSB) -> Little endian machine.
Data[0]: 2 - Data[1]: 1
After GET: Data[0]: 1 - Data[1]: 2
Thanks, Oliver
I've searched this group for Big/Little endian issues, don't kill me,
I know endianess issues have been discussed a 1000 times. But my
question is a bit different:
I've seen the follwing function several times, it converts data stored
in Big Endian (BE) format into host native format (LE on LE machines,
BE on BE machines):
/* - this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}
I can understand why this function swaps bytes on a LE machine, but
why doesn't it alter 'data' on a BE machine? I've tried to understand
it with diagrams and everything, but my brain went just crazy! Can
anyone give me a simple explanation please?
And how would a function look like which does the "opposite": swap
bytes on a BE machine, don't change values on a LE machine (that is,
"read and convert LE data")?
myself when I understand the 1st question...
Here's the complete test program:
#include <stdlib.h>
#include <stdio.h>
/* This function is useful when reading and converting data which is
stored
in Big Endian Format.
- this code swaps the bytes on a Little Endian Machine
- this code returns 'data' unmodified on a Big Endian Machine.
*/
static short getShortBE (char *data)
{
return (short) ((data[0] << 8) | data[1]);
}
int main (int argc, char **argv)
{
int a;
char c;
short data1;
short data2;
char *d;
(void)argc;
(void)argv;
a = 0x01020304;
c = ((char *)&a)[0];
if (c == 1)
{
fprintf (stdout, "Integer a: %x - first byte: %x (MSB) -> Big
endian machine.\n", a, c);
}
else if (c == 4)
{
fprintf (stdout, "Integer a: %x - first byte: %x (LSB) -> Little
endian machine.\n", a, c);
}
else
{
fprintf (stdout, "Integer a: %x - first byte: %x -> A weirdo
machine.\n", a, c);
}
data1 = 0x0102;
d = ((char *)&data1);
fprintf (stdout, "Data[0]: %x - Data[1]: %x\n",
*d, *(d + 1));
data2 = getShortBE ((char *)&data1);
d = ((char *)&data2);
fprintf (stdout, "After GET: Data[0]: %x - Data[1]: %x\n",
*d, *(d + 1));
return 1;
}
Output:
-------
Integer a: 1020304 - first byte: 1 (MSB) -> Big endian machine../Endian
Data[0]: 1 - Data[1]: 2
After GET: Data[0]: 1 - Data[1]: 2
On Linux (i386):
Integer a: 1020304 - first byte: 4 (LSB) -> Little endian machine.
Data[0]: 2 - Data[1]: 1
After GET: Data[0]: 1 - Data[1]: 2
Thanks, Oliver