I have a file that I read into a char array.
The char array now holds a certain number of bytes, now if I know the file stored a 32bit integer in little endian at position say 0x8, how do I retrieve it from the char array?
FILE * file = fopen("file");
char buffer[size];
fread(buffer,1,size,file);
int = buffer[0x8]; // What should I do here?
// I imagine it involves some strange pointer
// arithmetic but I can't see what I should do,
// casting to int just takes the first byte,
// casting to (int *) doesn't compile
you need to cast it like so:
Which will first cast the spot to a int pointer and the dereference it to the int itself.
[watch out for byte-ordering across different machine types though; some do high bytes first some do low]
And just to make sure the example is well understood, here’s some code showing the results:
And the results from running it: