Home > Software design >  byte order of 16 bit field in C
byte order of 16 bit field in C

Time:02-18

Assume I have a 16 bit or 4 hex characters field and data in it is in little endian

This is little endian

 B8 00 00 00

I want big endian of above

Should it be

 00 00 00 B8

or

what?

or If I have char 1 byte "0x1D" should it be same?

or if I have array[2]={0xiD,2E} what would be big endian data should it be of the following.

CodePudding user response:

Little Endian representation of value 0x000000B8 at address N:

  • Value at address N 0 is 0xB8
  • Value at address N 1 is 0x00
  • Value at address N 2 is 0x00
  • Value at address N 3 is 0x00

Big Endian representation of value 0x000000B8 at address N:

  • Value at address N 0 is 0x00
  • Value at address N 1 is 0x00
  • Value at address N 2 is 0x00
  • Value at address N 3 is 0xB8

CodePudding user response:

Should it be 00 00 00 B8

If this is the binary representation of a 32 bit integer, then yes.

If I have char 1 byte "0x1D" should it be same?

Endianess only applies to words (2, 4, 8 bytes data types), not to bytes. That is, integer or floating point types.

if I have array[2]={0xiD,2E} what would be big endian data

That doesn't make sense unless this is the binary representation of a larger type. Endianess doesn't apply to raw data (or strings).

More info here: What is CPU endianness?

  • Related