views:

43

answers:

2

Do I have to worry about endianness in this case (integers MUST be 0-127):

    int a = 120;
    int b = 100;
    int c = 50;
    char theBytes[] = {a, b, c};

I think that, since each integer sits in its own byte, I don't have to worry about Endianess in passing the byte array between systems. This has also worked out empirically. Am I missing something?

+2  A: 

No, you don't need to worry, compiler produces code which makes correct casting and assignment.

Alex Farber
+4  A: 

Endianness only affects the ordering of bytes within an individual value. Individual bytes are not subject to endian issues, and arrays are always sequential, so byte arrays are the same on big- and little-endian architectures.

Note that this doesn't necessarily mean that only using chars will make datatypes 100% byte-portable. Structs may still include architecture-dependent padding, for example, and one system may have unsigned chars while another uses signed (though I see you sidestep this by only allowing 0-127).

Chuck
Everything I wanted in an answer.
Yar