Skip to content

Instantly share code, notes, and snippets.

@joshbc
Created September 16, 2015 14:03
Show Gist options
  • Save joshbc/aa0ff12c54e1b0463349 to your computer and use it in GitHub Desktop.
Save joshbc/aa0ff12c54e1b0463349 to your computer and use it in GitHub Desktop.
unsigned char array to long conversion
typedef int32_t LONG;
const unsigned char* pData[4];
int pReadIndex = 0;
//unsafe
LONG *p_long = reinterpret_cast< LONG (&)[4] >( pData[pReadIndex] );
//safe
LONG aLong = reinterpret_cast<LONG>(pData[pReadIndex++]) |
reinterpret_cast<LONG>(pData[pReadIndex++]) << 8 |
reinterpret_cast<LONG>(pData[pReadIndex++]) << 16 |
reinterpret_cast<LONG>(pData[pReadIndex++]) << 24;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment