Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array?
I was looking for an answer but didn\'t get a
You're storing as ASCII, where you should be storing the bytes themselves.
The encoding should be something like:
uint32_t a = 0xff00ffaa;
unsigned char byte_array[1024];
Notice how I made your target array unsigned, to indicate that it's "raw bytes", and not actually characters.
byte_array[0] = a >> 24;
byte_array[1] = a >> 16;
byte_array[2] = a >> 8;
byte_array[3] = a >> 0;
This serializes the variable a
into the four first bytes of byte_array
using big-endian byte ordering, which is sort of the default for many network protocols.
You may also want to see my answer here: question 1577161.