Question

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.

Was it helpful?

Solution

After you provided more info, your code is OK for me. It prints the output:

00 08 00

First 00 is from unsigned char a; and second bytes 08 00 are from short. They are switched because of platform dependent data storing in memory.

If you want switched bytes of the short you could just show a short:

fprintf(stderr, "%02x %02x", (someStruct.myShort >> 8) & 0xFF, someStruct.myShort & 0xFF)

OTHER TIPS

I don't see a problem with your code. However, I get 08 00, which makes sense on my little-endian Intel machine.

The problem is in the format of the printf

%02x

%02x means that the result will be printed as hex value (x), with a minimum lenght of 2 (2) and filling the spaces with 0 (0)

Try with

fprintf(stderr, "%04x ", buffer[count]); 

The width specifier in the format string (2 in your case) refers to the minimum number of characters in the text output, not the number of bytes to print. Try using "%04x " as your format string instead.

As for the digit grouping (00 08 as opposed to 0008): Plain old printf doesn't support that, but POSIX printf does. Info here: Digit grouping in C's printf

Need to take care not to shift in a signed bit should buffer be signed. Use "hh" to only print 1 byte worth of data. "hh" available with C99. See What is the purpose of the h and hh modifiers for printf?

fprintf(stderr, "%02hhx %02hhx", buffer[count] >> 8, buffer[count]);

[Edit OP's latest edit wants to see 3 bytes] This will print all field's contents. Each field is in the endian order of the machine.

size_t len = sizeof(someStruct);
const unsigned char *buffer = (unsigned char*)&someStruct;
size_t count;
for(count = 0; count < len; count++) {
    fprintf(stderr, "%02x ", buffer[count]);
}
fprintf(stderr, "\n");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top