1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
unsigned int toUInt(const unsigned char bytes[4]){
unsigned int ret;
*((unsigned char*)&ret) = bytes[3];
*((unsigned char*)&ret + 1) = bytes[2];
*((unsigned char*)&ret + 2) = bytes[1];
*((unsigned char*)&ret + 3) = bytes[0];
return ret;
}
int main(void){
// example
unsigned int a = 0x0F00F0FFu;
unsigned char *b;
b = (unsigned char *)&a;
printf("a : %u, b0 : %u, b1 : %u, b2 : %u, b3 : %u\n", a, b[0], b[1], b[2], b[3]);
a = toUInt(b); // or toUInt((unsigned char *)&a)
printf("a : %u, b0 : %u, b1 : %u, b2 : %u, b3 : %u\n", a, b[0], b[1], b[2], b[3]);
return 0;
}
|