Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

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

views
     
tboxmy
post Jun 13 2023, 06:49 PM

Casual
***
Junior Member
478 posts

Joined: Oct 2006


Such an interesting questing. Allow me to share some info and a suggestion.

1. 64 bit unsigned integer or uint64 minimum value = 0 and max (2^64)-1
2. format-hex tool is available in MS Windows Power tools to display input as hexadecimal values.
3. The solution requires saving data as binary, and looks like another app will pick it up.
4. Without a C compiler at hand, can imagine how little endian will result, Better still, accessed compiler at https://www.onlinegdb.com/online_c_compiler#

Suggestion:
1. Store the integer in a fixed length to allow consistency of value retrieval
2. Working on top of code by @ cytyler, storing of values can be demonstrated

CODE

#include <stdio.h>

int main()
{
   unsigned long long long_integer1 = 0x0000001234567890;
   unsigned long long long_integer2 = 0x0000001234567890;
   FILE *fptr;
   printf("Hello world");
   fptr = fopen("demointeger.bin", "wb");
   fwrite(&long_integer1, sizeof(unsigned long long), 1, fptr);
   fwrite(&long_integer2, sizeof(unsigned long long), 1, fptr);
   fclose(fptr);
   return 0;
}


Output

CODE

C2 90 78 56 34 12 00 00 00 C2 90 78 56 34 12 00
00 00


Hope it helps.

user posted image

user posted image
tboxmy
post Jun 16 2023, 06:20 PM

Casual
***
Junior Member
478 posts

Joined: Oct 2006


QUOTE(flashang @ Jun 14 2023, 10:02 PM)
This is the result from @tboxmy code :

demointeger.bin (16 bytes)

90 78 56 34 12 00 00 00 90 78 56 34 12 00 00 00

Don't know why (and how) to get the different result.

smile.gif
*
user posted image

1. I dont have a PC with C compilers anymore...sigh! The online Compiler in use is https://www.onlinegdb.com/online_c_compiler# where I suspect it compiles to UTF.
2. When compile with LANG or LC_ALL default on Linux would be LANG=C. However, where LANG=en_US.UTF-8 then the hex values will differ as what I have shown. LC_TYPE value is connected too, need to check if you plan to use utf8 (which is widely used on global wide applications).
3. If you execute/run this a.out or binary, it should display the unicode as shown in attachment.
4. The unicode should display as shown by E2 98 A0, where you can refer at https://www.utf8-chartable.de/unicode-utf8-...rt=9728&names=-

CODE

#include <limits.h>
#include <locale.h>
#include <stdio.h>

int main()
{
  unsigned long long long_integer1 = 0x0000001234567890;
  unsigned long long long_integer2 = 0x0000001234FFFFFF;
  const struct lconv * const currentlocale = localeconv();
  FILE *fptr;
  printf("Hello world");
  printf("\n%d bits \%s\n", (int)(CHAR_BIT * sizeof(void *)), "\xE2\x98\xA0");
//   str=getlocale(LC_CTYPE);
  fptr = fopen("demointeger.bin", "wb");
  fwrite(&long_integer1, sizeof(unsigned long long), 1, fptr);
  fwrite(&long_integer2, sizeof(unsigned long long), 1, fptr);
  fclose(fptr);
  return 0;
}


Lots of this is just off my mind. Do read up for actual reasons.

This post has been edited by tboxmy: Jun 16 2023, 06:35 PM
tboxmy
post Jun 16 2023, 06:25 PM

Casual
***
Junior Member
478 posts

Joined: Oct 2006


user posted image

Using the online hex viewer, https://www.scadacore.com/tools/programming...-hex-converter/

Attached is the output, and how that would have shown 0x0000001234567890 in its utf-8 instead of ansi ascii.
Hope that helps.

This post has been edited by tboxmy: Jun 17 2023, 09:43 PM

 

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