Hexadecimal representation on little and big endian systems

0

Consider the following code :

unsigned char byte = 0x01;

In C/C++ the hexadecimal will be considered as an int, and therefore will be expanded to more then one byte. Because there is more then one byte, if the system uses little or big endian has effect. My question is to what this will be expanded?

If the system has int that contains 4 bytes, to which of the following it will be expanded :

0x00000001 OR 0x01000000

And does the endianness effect to which of them it will be expanded?

c++
c
hex
endianness
asked on Stack Overflow Oct 24, 2020 by rrrrTTTrrrr • edited Oct 24, 2020 by csavvy

2 Answers

1

The endianness does't matter. The endianness only relates to how the value is stored in memory. But the value represented by what's stored in memory will be 0x01, no matter how it is stored.

Take for example the value 0x01020304. It can be stored in memory as either 01 02 03 04 or 04 03 02 01. But in both cases it's still the value 0x01020304 (or 16 909 060).

answered on Stack Overflow Oct 24, 2020 by Darhuuk
1

It is completely architecture specific. The compiler produces the output according to your computer's endianness. Assuming that your microprocessor is big-endian, the compiler will produce;

char x = 0x01; --> 01,
int x = 0x01;  --> 00 00 00 01

On the other hand, if you use a cross compiler that targets little-endian or you compile the code on a little-endian machine, it will produce;

char x = 0x01; --> 01
int x = 0x01;  --> 01 00 00 00

Summarily, there is no standard or default endianness for the output, the output will be specific to the architecture which the compiler targets to.


User contributions licensed under CC BY-SA 3.0