If you really want to extract individual bytes first:
unsigned char a = orig & 0xff; unsigned char b = (orig >> 8) & 0xff; unsigned char c = (orig >> 16) & 0xff; unsigned char d = (orig >> 24) & 0xff;
Or:
unsigned char *chars = (unsigned char *)(&orig); unsigned char a = chars[0]; unsigned char b = chars[1]; unsigned char c = chars[2]; unsigned char d = chars[3];
Or use the union of unsigned long and four chars:
union charSplitter { struct { unsigned char a, b, c, d; } charValues; unsigned int intValue; }; charSplitter splitter; splitter.intValue = orig;
Update: as Friol pointed out, solutions 2 and 3 are not agnostic; which bytes a , b , c and d represent a dependency on the CPU architecture.
source share