Byte ordering and array access



Hello all,
I've heard differing opinions on this and would like a definitive
answer on this once and for all. If I have an array of 4 1-byte values
where index 0 is the least signficant byte of a 4-byte value. Can I use
the arithmatic shift operators to hide the endian-ness of the
underlying processor when assembling a native 4-byte value like
follows:

unsigned int integerValue;
unsigned char byteArray[4];

/* byteArray is populated elsewhere, least signficant byte in index 0,
guaranteed */

integerValue = (unsigned int)byteArray[0] |
((unsigned int)byteArray[1] << 8) |
((unsigned int)byteArray[2] << 16) |
((unsigned int)byteArray[3] << 24);

So if byteArray[0] was 0x78, byteArray[1] was 0x56, byteArray[2] was
0x34 and byteArray[3] was 0x12 then would integerValue be 0x12345678 no
matter the endian-ness of the processor?

Thanks in advance,

Ben

.



Relevant Pages

  • Re: Byte ordering and array access
    ... where index 0 is the least signficant byte of a 4-byte value. ... the arithmatic shift operators to hide the endian-ness of the ... underlying processor when assembling a native 4-byte value like ...
    (comp.lang.c)
  • Re: Byte ordering and array access
    ... where index 0 is the least signficant byte of a 4-byte value. ... the arithmatic shift operators to hide the endian-ness of the ...
    (comp.lang.c)
  • Re: Byte ordering and array access
    ... the arithmatic shift operators to hide the endian-ness of the ... underlying processor when assembling a native 4-byte value like ...
    (comp.lang.c)
  • Re: Byte ordering and array access
    ... the arithmatic shift operators to hide the endian-ness of the ... underlying processor when assembling a native 4-byte value like ... unsigned int integerValue; ... unsigned char byteArray; ...
    (comp.lang.c)