What's with the zeros in my binary?

So. I experimented with fwrite ().

On my system, sizeof (int) = 4. I have an ints array that contains: 1, 2, 3, 4, 5, and 6.

When I write it to a binary and view it with hexdump, I get:

0000000 0001 0000 0002 0000 0003 0000 0004 0000 0000010 0005 0000 0006 0000 0000018 

What does it write zeros between 4 byte values?

+4
source share
3 answers

I think you misunderstand how big the byte in your release is - 8 bits require a full representation of two hexadecimal digits. One of the int from your example:

 0001 0000 

You might want to display as 32-bit data (or 8-bit data) rather than 16. Which makes your landfill weird.

I duplicated your binary and ran od several ways. I hope you find an example that can educate:

 $ od -t x4 example 0000000 00000001 00000002 00000003 00000004 0000020 00000005 00000006 0000030 $ od -t x2 example 0000000 0001 0000 0002 0000 0003 0000 0004 0000 0000020 0005 0000 0006 0000 0000030 $ od -t x1 example 0000000 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 0000020 05 00 00 00 06 00 00 00 0000030 

As you can see best from the 1- and 4-byte examples, I am also on a small machine, just like you.

+2
source

Because 1 (for example), represented as a 4-byte hex, is 00000001 . Obviously, you are on a little-endian system, hence the reverse order appears to be when you check your file.

+4
source

Your hexdump groups two bytes as one word and changes the continent. On most systems, using hexdump -C changes the dump into a canonical representation, which prevents grouping. In hexadecimal, one character represents one nybble, and there are two nybbles per byte. So your 4-byte int should have only 8 nybbles. Since your numbers are very small, most of your nybbles are 0.

+3
source

All Articles