So, my question is pretty simple:
I need to fill a char/unsigned char array with some information. Some values in the middle are taken from short/int types and this is what happens:
Code:
int foo = 15; //0x0000000F
unsigned char buffer[100]={0};
..
memcpy(&buffer[offset], &foo, sizeof(int)); //either memmove
...
Output:
... 0F 00 00 00 ..
So by now I wrote a function to reverse this fields, but I don't find this a smart solution, as it impacts execution time, resources, and time to develop.
Is there an easier way to do it?
Edit: As many of you have pointed, this behaviour is produced due to the little endian processor, but my problem still remains. I need to fill this buffer with int/short values in big-endian, as i need to serialize tha data to be transmitted to a machine which either works in little/big endian, doesn't matter as this protocol is already defined so.
Note: For compiling in C++