473,772 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.mod erated - moderation address: cl**@plethora.n et -- 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 3590
ae****@yahoo.co m 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.mod erated - moderation address: cl**@plethora.n et -- 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.mod erated ae****@yahoo.co m 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.mod erated - moderation address: cl**@plethora.n et -- 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.co m 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_F ILE;

return var;
}

Of course there are 1001 (at least) other ways to write
this function.
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et -- 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
3100
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
1977
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' function returns the file dsc 3. So this seems ok.. Any idea what might be wrong?
2
1614
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 "B" contains one variable "Private" "VarB". How I can read the value of "VarB" from form "C"? All the form come loaded with the following brace of instructions: Sub Button2_Click(ByVal sender As System.Object, ByVal and As System.EventArgs)...
6
6007
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 assign a value to them. I wanted to trap for this exception in a subclass using super but it doesn't happen. I have read Guido's tutorial on new style classes and Shalabh's tuturial on new style attributes and methods, and thought I understood...
4
7901
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: <Interface> <Product> <CategoryFeatureGroup ID="622" No="1"> <FeatureGroup ID="0">
17
5414
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 think it is mostly not going to happen for ++ operation since it is not something like read a value and wait sometime then write back in multiple threading environment, BTW, is this understanding right?).
15
5958
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, but I get an error that says the index isn't defined. The second two calls are AJAX-generated. The second call immediately echos the $_SESSION variable back after it sets it, and it sets it properly. But the subsequent request doesn't see...
4
4890
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 dynamic create text-field "txtName". I thanks...
1
3367
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 the iteration variable is used and you cannot modify it. For example: int pins = {9, 3, 7, 2} int newPin = 10; foreach(int pin in pins) {
2
1438
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 form below. I know the basics about IF conditions and have read plenty of them without difficulty but this one is really confusing me. There is a long, complicated IF condition (1 AND and 2 ORS, with 2 parts to each OR and 2 NOTs!), then a list...
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.