Suppose I have a C-style array of type unsigned char
:
unsigned char * c = (unsigned char *) malloc(5000 * sizeof(unsigned char));
for(int i = 0; i < 5000; i++)
c[i] = (unsigned char) ((i >> (i%4 * 8)) & 0xFF);
Suppose I have a pointer offset to a position which starts a 4 byte integer:
// pseudo code
unsigned int i = c + 10; // 10 = pointer offset, let's say.
If I want to load i
up with the correct number, I can do:
unsigned int i = (*(c+10) << 24) + (*(c+11) << 16) + (*(c+12) << 8) + (*(c+13));
But shouldn't I just be able to, somehow, do this using casts?
// pseudo code -- I haven't gotten this to work yet:
int i = (unsigned int) (*((void *)(c+10));
// or maybe
int i = *((unsigned int*)((void *)(c+10)));
In short, what is the cleanest, most effective way to transition the four bytes to an unsigned int
in a C-style byte array?