#include <stdio.h>
#include <stdlib.h>
int main(){
int n = 56789000;
unsigned char bytes[4];
bytes[0] = (n >> 24) & 0xFF;
bytes[1] = (n >> 16) & 0xFF;
bytes[2] = (n >> 8) & 0xFF;
bytes[3] = n & 0xFF;
int test = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | (bytes[0]);
printf("%d\n",n);
printf("%d\n", test);
}
Output is:
56789000
143155715
Hello,
I'm trying to store an integer into an unsigned char array and want to convert it back to an integer later. I found some code-snippets which lead me to the code above but the output is not as expected. Can you please help to fix the code above. I'm not very familiar with C and don't know what is wrong with the code.
Thank in advance