Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

C How does C/C++ store integer in binary file?, Endianness in x86

views
     
cytyler
post May 24 2023, 08:16 AM

Casual
***
Junior Member
399 posts

Joined: Nov 2014
For 0x1234:

In memory, the least significant byte is 34, and the most significant byte is 12.
When written to the file in binary format, the bytes are written in little-endian order.
Therefore, the hexdump shows 34 12 00 00, with the least significant byte 34 appearing first.

For 0x12345678:

In memory, the least significant byte is 78, and the most significant byte is 12.
When written to the file in binary format, the bytes are written in little-endian order.
Therefore, the hexdump shows 78 56 34 12, with the least significant byte 78 appearing first.


--

To save int only can save to text, binary

This post has been edited by cytyler: May 24 2023, 08:18 AM
cytyler
post May 24 2023, 08:28 AM

Casual
***
Junior Member
399 posts

Joined: Nov 2014
QUOTE(Tullamarine @ May 24 2023, 08:23 AM)
Thanks to both for you reply. Some how I cannot make my C code work to write 64-bit integer to file.

Can you show how this long integer will be store in binary file: 0x0000001234567890

Or demonstrate any number in 64-bit integer, preferably with leading zero.

Thanks once again!
*
#include <stdio.h>

int main()
{
unsigned long long long_integer = 0x0000001234567890;
FILE *fptr;

fptr = fopen("C:\\Users\\BOO\\Projects\\integer.bin", "wb");


fwrite(&long_integer, sizeof(unsigned long long), 1, fptr);

fclose(fptr);

return 0;
}


 

Change to:
| Lo-Fi Version
0.0163sec    0.25    6 queries    GZIP Disabled
Time is now: 24th December 2025 - 04:45 AM