473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

read 32 bit value into 64 bit variable?

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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Nov 15 '05 #1
4 3569
ae****@yahoo.com wrote:
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);
Nov 15 '05 #2
On Sun, 26 Jun 2005 09:57:52 +0000, ae5880 wrote:
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.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Nov 15 '05 #3
In comp.lang.c.moderated ae****@yahoo.com wrote:
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]

--
Hans-Bernhard Broeker (br*****@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Nov 15 '05 #4
ae****@yahoo.com wrote:

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.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
9
by: ferbar | last post by:
Hi all, I'm trying to read from the txt file 'ip.packets.2.txt' using the read function. It seems everything ok, but I get a -1 when executing >>bytesr = read(fdo1, bufread, 2); The 'open'...
2
by: dumbo | last post by:
Hello, who gives one more shining solution to me? My application load form "A". The form "A" by means of a push-button load form "B" that in its turn by means of a push-button load form "C". Form...
6
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to...
4
by: Pim75 | last post by:
Hello, I have to read a XML file in ASP and save the values in a database. I can get this work, but I cannot read some nested nodes of the xml file. This is a part of the XML file: ...
17
by: Ryan Liu | last post by:
Hi, If I have many threads write to a variable(e.g. var++) and another thread read it on an interval base. For those writing thread, I know I need lock, or its value could be lower ( even I...
15
by: Evil Otto | last post by:
My page loads, and calls an init() function that returns content to a div on the page, as well as setting a $_SESSION variable. The content it returns includes a link that calls the same variable,...
4
by: Michael Munch | last post by:
Hi I want to read the value of af text-field, create dynamic, in a form. Se below a small test-site to do that (but readning fails): I use the function Test_Read for reading the value from the...
1
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
2
by: new2c | last post by:
Hi - I'm completely new to C and trying to read some code that doesn't make sense to me. For proprietary reasons I can't display the code here but I'll describe it...and list part of it in a generic...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.