Okay so I am trying to print hexadecimal values of a struct. Now my print function does the following:
int len = sizeof(someStruct);
unsigned char *buffer = (unsigned char*)&someStruct;
int count;
for(count = 0; count < len; count++) {
fprintf(stderr, "%02x ", buffer[count]);
}
fprintf(stderr, "\n");
Here is the definition of the struct:
struct someStruct {
unsigned char a;
short myShort;
} __attribute__((packed)) someStruct;
The length of this struct printed out as expected is (output on console):
sizeof(someStruct): 3 bytes
Issue here is the following that I am encountering. There is a short which I set to a value.
someStruct.myShort = 0x08;
Now this short is 2 bytes long. When it is printed out into the console however, it does not show the most significant 0x00. Here is the output I get,
stderr: 00 08
I would like the following output however (3 bytes long),
stderr: 00 00 08
If I fill the short with a 0xFFFF, then I do get the 2 byte output, however, whenever there is leading 0x00, it does not output the leading 0x00 to console.
Any ideas on what I am doing wrong. Probably something small I would assume I am overlooking.