read 32 bit value into 64 bit variable?

A

ae5880

Hello all,

need to read a 321bit value from a file into a 64 bit variable (either
float or int). in addition, the endianness of the run-time platform
could differ from the endianness of the value - let's assume it's known
at runtime whether the endianness is the same or different. any ideas?

TIA,
andrey
 
G

Grumble

need to read a 32 bit value from a file into a 64 bit variable (either
float or int). in addition, the endianness of the run-time platform
could differ from the endianness of the value - let's assume it's known
at runtime whether the endianness is the same or different. any ideas?

Assume your value is 1,500,000,000.

int i = 1500000000; /* 32-bits wide ints */
fprintf(fp, "%i\n", i);

then, on the other system,

int i; /* 64-bits wide ints */
fscanf(fp, "%i\n", &i);

or

float f;
fscanf(fp, "%f\n", &f);
 
N

Netocrat

Hello all,

need to read a 321bit value from a file into a 64 bit variable (either
float or int). in addition, the endianness of the run-time platform could
differ from the endianness of the value - let's assume it's known at
runtime whether the endianness is the same or different. any ideas?

If the file representation of the 32-bit value doesn't change between
run-time platforms then you don't need to worry how the machine represents
it in memory. Just don't use any techniques that make any assumptions
about byte-position - eg. use bit shifting if you have to interpret the
32-bit value in the file as 4 separate bytes.
 
H

Hans-Bernhard Broeker

In said:
need to read a 321bit value from a file into a 64 bit variable (either
float or int). in addition, the endianness of the run-time platform
could differ from the endianness of the value - let's assume it's known
at runtime whether the endianness is the same or different. any ideas?

Read it byte-wise, cast each byte to the result type, shift it to the
right position, and logical-or it all together.

[And please don't cross-post between an open and a moderated group; or
if you think you have to, at least reduce the F'up2 to a single group.
Thank you]
 
O

Old Wolf

need to read a 321bit value from a file into a 64 bit variable (either
float or int). in addition, the endianness of the run-time platform
could differ from the endianness of the value - let's assume it's known
at runtime whether the endianness is the same or different. any ideas?

You have to know the endianness of the source file.
Assuming it's big-endian and your PC has 8-bit chars:

long long read_64(FILE *fp)
{
unsigned long long var;
var = getc(fp); var <<= 8;
var |= getc(fp); var <<= 8;
var |= getc(fp); var <<= 8;
var |= getc(fp);

if (feof(fp))
return ERROR_READING_FILE;

return var;
}

Of course there are 1001 (at least) other ways to write
this function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top