0

Im trying to convert little endian to big endian and having trouble. I have an array that has 2 bytes of unsigned char which is hex values. and i would like to make it big endian by the zeros are gone and i cant get the values i want.

For example, My goal is to get

array[0] = 0b
array[1] = 40
output = 0b40 so i can see this value as 2880 when %d

so i used

output = (array[1]>>4) | (array[0]<<4);

but in reality, i see

printf("output = %x04\n", output);
output = 00b4

I want that output to be 0b40 so i can get 2880 when i do

printf("output = %x\n", output);
output = 0b40

printf("output = %d\n", output);
output = 2880

thanks guys any help will be appreciated

Jay Cho
  • 21
  • 2

1 Answers1

0

You need to shift the bottom up by eight bits, not four, and not shift the top:

output = (array[0]<<8) | array[1];

See convert big endian to little endian in C for more discussion and code.

Community
  • 1
  • 1
RichieHindle
  • 272,464
  • 47
  • 358
  • 399