Total members 11892 |It is currently Sun Sep 08, 2024 4:53 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





I have a program that writes a binary config file for another piece
of hardware. One of the requirements of the file is that integer
values are 32 bits. Currently the file is created as expected when
run on a 32 bit machine. However, I need to make it portable, so
that it could also be run on a machine that uses 64 bit ints, and
I'm not sure how to do it.

Currently, the code is something like this:

Code:
struct Data
{
int nInt1;
int nInt2;
char czString[32];
};


...

Code:
FILE* pFile = fopen("filename.bin", "wb");
Data data;
// do stuff

fwrite(&data, sizeof(data), 1, pFile);


...

Now, I know this is not portable because of int size (as well as
possible packing issues) so I thought about changing it to something
like this:

Code:
const int INT_SIZE = 4;
const int STRING_LENGTH = 32;
struct Data
{
int nInt1;
int nInt2;
char czString[STRING_LENGTH];
};

...

FILE* pFile = fopen("filename.bin", "wb");
Data data;
// do stuff
Code:
fwrite(&data.nInt1, INT_SIZE, 1, pFile);
fwrite(&data.nInt2, INT_SIZE, 1, pFile);
fwrite(&data.czString, 1, STRING_LENGTH, pFile);

...

Will this actually work? Will this write the lower 32 bits of an
int if it is 64 bits? If not, how can I guarantee it will write the
lower 32 bits?




Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

A 32-bit integer can be represented as an array of 4 chars,
assuming 8 bits per byte. Conversion between ordinary
integers and this array can be done by bitwise
mask-and-shift operations. The following should work for any
unsigned integer type >= 32 bits; I've used long long here:

Code:
#include <stdio.h>
#include <limits.h>

int main(void)
{
unsigned long long i = 1234567890;
unsigned char c[4];

printf("Your integers are %i bits\n", CHAR_BIT *
sizeof(i));

c[0] = i & 0xff; /* least significant byte */
c[1] = (i & 0xff00) >> 8;
c[2] = (i & 0xff0000) >> 16;
c[3] = (i & 0xff000000) >> 24;

printf("0x%08x == ", i);
printf("0x%02x%02x%02x%02x", c[3], c[2], c[1], c[0]);

return 0;
}

On my Win32 system this prints

Your integers are 64 bits
0x499602d2 == 0x499602d2

The first hex value is derived from i, the second from c[],
and they should be the same. I don't have a 64-bit machine
to try this on; maybe someone who has would oblige!

(To write binary data from c[] you'd need a different format
string of course.)

There are also considerations of endianess to be taken into
account.

_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Ensuring integer are written as 32 bits
 Bit operations-set-get-xor-rotate on bits arrays     -  
 in what language is C++ and Java are written?     -  
 How meta tags can be written in HTML     -  
 integer value overflow     -  
 Dividing two Integer values     -  
 Invalid integer formats     -  
 finding the largest integer     -  
 handle integer overflows and underflows     -  
 rounding performed under integer division     -  
 Integer value compare with equal sign     -  



Topic Tags

C++ Variables






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com