
Untitled
By: a guest on
May 1st, 2012 | syntax:
None | size: 1.07 KB | hits: 17 | expires: Never
Get int from char[] of bytes
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
int foo = *((int *) &buffer[0x8]);
#include <stdio.h>
main() {
char buffer[14] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13 };
int foo = *((int *) &buffer[0x8]);
int bar = (int) buffer[0x8];
printf("raw: %dn", buffer[8]);
printf("foo: %dn", foo);
printf("bar: %dn", bar);
}
raw: 8
foo: 185207048
bar: 8
int x = *(int *)&buffer[8];
int x = buffer[8] + (buffer[9] << 8) + (buffer[10] << 16) + (buffer[11] << 24);
int val = 0x00000000;
char* ptr = reinterpret_cast<char*>(&val);
*ptr = buffer[0x0a];
ptr++;
*ptr = buffer[0x0b];
ptr++;
*ptr = buffer[0x08];
ptr++;
*ptr = buffer[0x09];
ptr++;
int *list = (int*)buffer;
int n = fread(buffer, 1, size, file);
for (int i=0; i< n/sizeof(int); i++)
printf("%dn", list[i]);