I've been reading this thread Store an int in a char array?
And I need to store the int in the array of chars.
So I read the previous thread and I tried to make my own demo. But it's not working, trying to figure out why not for a long time. Maybe you could give me some clue or ideas please?
#include <stdio.h>
int main(void) {
char buffer[4];
int y = 2200;
buffer[0] = (y >> 0) & 0xff;
buffer[1] = (y >> 8) & 0xff;
buffer[2] = (y >> 16) & 0xff;
buffer[3] = (y >> 24) & 0xff;
int x = buffer[0];
printf("%i = %i\n", y, x);
}
Output
gcc tmp.c && ./a.out
2200 = -104