0

I have an uint32_t* pointer charData set to data that is stored in LE order. When I simply dereference the pointer and store it in a uint32_t it is interpreted falsely (i.e. as Big Endian). What's the best way to rotate the bytes in c++?

unsigned char arr[4] = {0x02, 0x11, 0x01, 0x6B };
unsigned char* charData = arr;
uint32_t value = *((uint32_t*) charData);
tzippy
  • 6,458
  • 30
  • 82
  • 151
  • Yes, I'm sorry that's what I meant by falsely. – tzippy Jul 18 '14 at 11:37
  • 1
    Possible anser: http://stackoverflow.com/questions/105252/how-do-i-convert-between-big-endian-and-little-endian-values-in-c – anderas Jul 18 '14 at 11:39
  • Code like this is used a lot but it's actually undefined behaviour. `*((uint32_t*) charData)` should be a `memcpy`. – Simple Jul 18 '14 at 11:48
  • Just do: `uint32_t value = (((*charData) & 0xFF) << 24) | (((*charData) & 0xFF00) << 8) | (((*charData) & 0xFF0000) >> 8) | ((*charData) >> 24);`, better is encapsulated in a method (ex: ChangeEndianess). – NetVipeC Jul 18 '14 at 11:56
  • @WhozCraig i took the array as an example. I have no influcence on th actual source for the data. – tzippy Jul 18 '14 at 11:56

0 Answers0