1

I am trying to use fwrite to write struct into binary file, but the bytes order of the binary file really confuses me. The struct is as follow:

(gdb) p data[0]@10
$1 = {{alphCount = 58, ascii = 97 'a'}, {alphCount = 1, ascii = 10 '\n'}, {alphCount = 31, ascii = 98 'b'}, {alphCount = 1,
    ascii = 10 '\n'}, {alphCount = 20, ascii = 99 'c'}, {alphCount = 1, ascii = 10 '\n'}, {alphCount = 31, ascii = 100 'd'}, {
    alphCount = 1, ascii = 10 '\n'}, {alphCount = 377, ascii = 101 'e'}, {alphCount = 1, ascii = 10 '\n'}}

And the result of hexdumping the binary file is as follow:

0000000 003a 0000 0161 0000 0a00 001f 0000 0162
0000010 0000 0a00 0014 0000 0163 0000 0a00 001f
0000020 0000 0164 0000 0a00 0179 0000 0165 0000
0000030 0a00                                   
0000032

In addition, I have used "__attribute__((packed))" to disable the alignment of struct, so the size of my struct would be 5 bytes.

And the fwrite code is as follow:

fwrite(data, 5, 10, stdout);

Why the first byte of the file is "00" rather than "3a"? In my knowledge, I am using the intel architecture, and the order of storing bytes would be little-endian, I thought the first few bytes of the file would be like "3a00 0000 61", but it actually isn't like what I thought.

Any advise would be appreciated, thanks in advance!

7
  • 1
    How is this struct declared? Commented Aug 10, 2022 at 15:04
  • Like this: struct dataStruct{ int alphCount; char ascii; }__attribute__((packed)); typedef struct dataStruct compressData; Commented Aug 10, 2022 at 15:19
  • Thats weird. This data is messed up in many ways. Commented Aug 10, 2022 at 15:23
  • You should show single bytes in your hex dump. 003a 0000 is 3a 00 00 00 in plain bytes which is little endian representation of intvalue 58==0x3A. 0161 is 61 01 where the first byte is 0x61 == 97. Then withoud padding the second struct start. Commented Aug 10, 2022 at 15:29
  • Yeah, that really confuses me. Actually, this question comes from when I am writing a little project of a OS course. Here is the project link link. The "wzip" part, to be precise. Actually, I have complete the project, passed the test script, and the answer file turns out the same as mine. Commented Aug 10, 2022 at 15:32

2 Answers 2

1

You are mixing representations:

Why the first byte of the file is "00" rather than "3a"? In my knowledge, I am using the intel architecture, and the order of storing bytes would be little-endian, I thought the first few bytes of the file would be like "3a00 0000 61", but it actually isn't like what I thought.

Well, yes. But you do not look at plain byte values. You are looking at 16bit words. For this the byte order has already be taken into account and the first 2 bytes 3a 00 are shown as 003a.

If you switch your hex dump tool to 32bit values you would probably get 0000003a xxxxxx61 and when you switch to single bytes you will get 3a 00 00 00 61

Sign up to request clarification or add additional context in comments.

1 Comment

Oh! I had never thought about that! Now I know the problem. Really thank you!
1

As you say, little-endian byte order is used here, so 003a is stored as 3a 00.

To be clear, the first byte is 3a, and the second 00.

I find the default format of hexdump incredibly poor. Perhaps you should be using hexdump -C or od -t x1 instead of just hexdump.

1 Comment

Yeah, I know what I have mixed up now. And also your advise really helps me out. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.