Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Binary file - numbers

Reply
Thread Tools

Binary file - numbers

 
 
E-Star
Guest
Posts: n/a
 
      06-27-2003
I have a binary file that I want to read some numbers out of. However
the numbers are 32bit floats. How can I get the numbers into a C
program to use? I can do the calculations manually....but is there an
easier way? Is there some handy functions which do this?

example.

File contains: 42 7C 00 00
I want to read it in and end up with my C variable equalling 63.
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      06-27-2003
E-Star <(E-Mail Removed)> wrote:

> I have a binary file that I want to read some numbers out of. However
> the numbers are 32bit floats. How can I get the numbers into a C
> program to use?


Well... you _could_ try fread(), but there's a catch. fread() reads
bytes into memory, not values. If those floats weren't written by a
system that arranges its floats exactly the same way as yours does, you
won't get the values you expected.
You can, of course, usually get away with having one program fwrite()
some floats, and then having another program on the same machine fread()
them later on, but if you want to read files from another machine,
you're taking a risk, and the risk is greater the less like your own
machine the source of the file is.
It's all a question of balancing demands, really. If you know that this
file is only ever going to contain "your" kind of floats, you opt for
simplicity and use fread(). If you know that this file is a cross-
platform standard and you want your program to run on several kinds of
machines, you parse it by hand.

Richard
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      06-27-2003
James Connell wrote:

> I'm still trying to figure out how *any* combo of 0x42, 0x7C, 0x0 and
> 0x0 == 63?????


It's a float representation for 63.0

--
pete
 
Reply With Quote
 
Phil Tregoning
Guest
Posts: n/a
 
      06-27-2003
pete <(E-Mail Removed)> wrote:

> James Connell wrote:
>
>> I'm still trying to figure out how *any* combo of 0x42, 0x7C, 0x0 and
>> 0x0 == 63?????

>
> It's a float representation for 63.0
>


Or more precisely, it's an IEEE 60559 single precision representation
for 63.0. The code below produces the output "63", and is based on the
description here:

http://www.psc.edu/general/software/...ieee/ieee.html

The code is not optimised or well tested.

For systems that actually use IEEE single floats, bear in mind that
they might have a different byte order.

Phil T



#include <stdio.h>
#include <math.h>
#include <float.h>
#include <assert.h>

double ieee_single(const void *v)
{
const unsigned char *data = v;
int s, e;
unsigned long src;
long f;
double value;

src = ((unsigned long)data[0] << 24) +
((unsigned long)data[1] << 16) +
((unsigned long)data[2] << +
((unsigned long)data[3]);

s = (src & 0x80000000UL) >> 31;
e = (src & 0x7F800000UL) >> 23;
f = (src & 0x007FFFFFUL);

if (e == 255 && f != 0) {
/* NaN - Not a number */
value = DBL_MAX;
}
else if (e == 255 && f == 0 && s == 1) {
/* Negative infinity */
value = -DBL_MAX;
}
else if (e == 255 && f == 0 && s == 0) {
/* Positive infinity */
value = DBL_MAX;
}
else if (e > 0 && e < 255) {
/* Normal number */
f += 0x00800000UL;
if (s) f = -f;
value = ldexp(f, e - 127 - 23);
}
else if (e == 0 && f != 0) {
/* Denormal number */
if (s) f = -f;
value = ldexp(f, -126 - 23);
}
else if (e == 0 && f == 0 && s == 1) {
/* Negative zero */
value = 0;
}
else if (e == 0 && f == 0 && s == 0) {
/* Positive zero */
value = 0;
}
else {
/* Never happens */
printf("s = %d, e = %d, f = %lu\n", s, e, f);
assert(!"Oops, unhandled case in ieee_single()");
}

return value;
}

int main(void)
{
float f;
unsigned char combo[] = {0x42, 0x7c, 0x00, 0x00};
printf("%g\n", ieee_single(combo));
return 0;
}
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-27-2003
James Connell wrote:
>

.... snip ...
> >

> I'm still trying to figure out how *any* combo of 0x42, 0x7C,
> 0x0 and 0x0 == 63?????


To start with 63 has (in binary) 6 successive 1 bits. Most FP
representations suppress the MS bit, and replace it with a sign
bit, leaving 5 bits. Where does your representation have 5
successive 1 bits? That would appear to locate the significand.
Most systems also use an offset method for the exponent, such that
the common integers are in the middle of the range. That would
appear to locate the exponent portion. Now all you have left to
worry about is the order of those two 0x0 bytes. A simple
experiment or two should resolve that.

The representation appears to be: sign, 8 bit binary exponent
offset 128, 23 bit significand, in order. Looks awfully IEEEish
to me.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Dhruv
Guest
Posts: n/a
 
      06-27-2003
On Fri, 27 Jun 2003 01:39:54 -0600, E-Star wrote:

> I have a binary file that I want to read some numbers out of. However
> the numbers are 32bit floats. How can I get the numbers into a C
> program to use? I can do the calculations manually....but is there an
> easier way? Is there some handy functions which do this?
>
> example.
>
> File contains: 42 7C 00 00
> I want to read it in and end up with my C variable equalling 63.



If your program stored them in the standard IEEE floating point format,
then you could find out which bits represents what (number, sign,
exponent), and rotate the appropriate bits, and get the equivalent 16-bit
float. You would have been better off storing the number as ASCII text, if you
were worried about portability.

-Dhruv.






 
Reply With Quote
 
James Connell
Guest
Posts: n/a
 
      06-27-2003
pete wrote:
> James Connell wrote:
>
>
>>I'm still trying to figure out how *any* combo of 0x42, 0x7C, 0x0 and
>>0x0 == 63?????

>
>
> It's a float representation for 63.0
>


ok - i missed the 'float' in his post.

 
Reply With Quote
 
Jim Fischer
Guest
Posts: n/a
 
      06-27-2003
James Connell wrote:
> I'm still trying to figure out how *any* combo of 0x42, 0x7C, 0x0 and
> 0x0 == 63?????


Floating-point values are commonly implemented internally in accordance
with IEEE Standard 754 for Binary Floating-Point Arithmetic (IEEE 754).
Assuming your floating-point values are in fact IEEE 754 compliant (and
they probably are), take a look at the following web site:

http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html

This site shows how base-10 floating-point values are represented in
IEEE 754 format, and vice versa.

FWIW, here's another web site with IEEE 754 info if you're interested:

http://www.sns.ias.edu/Main/info//sw...hop/common/ug/

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-27-2003
E-Star <(E-Mail Removed)> wrote (27 Jun 2003) in
news:270620030139549660%(E-Mail Removed) / comp.lang.c:

> I have a binary file that I want to read some numbers out of. However
> the numbers are 32bit floats. How can I get the numbers into a C
> program to use? I can do the calculations manually....but is there an
> easier way? Is there some handy functions which do this?
>
> example.
>
> File contains: 42 7C 00 00
> I want to read it in and end up with my C variable equalling 63.


If you use fread() on the file -- opened as a binary file -- you can get
the bytes into a char[]. Just using memmove or memcpy to move them to a
float (or freading into a float) may nor suffice. For example, on my
implementation the bytes for 63.0f are { 0x0, 0x0, 0x7c, 0x42 }, so
would require reordering before using memmove to transfer them to the
float.



--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America
 
Reply With Quote
 
E-Star
Guest
Posts: n/a
 
      06-27-2003
Just to let everyone know...for simplicity I did write the bytes in
reverse.

ie. The file really contains 00 00 7C 42



In article <Xns93A7915164BC3PhilipTregoningesain@131.176.85.3 6>, Phil
Tregoning <(E-Mail Removed)> wrote:

> pete <(E-Mail Removed)> wrote:
>
> > James Connell wrote:
> >
> >> I'm still trying to figure out how *any* combo of 0x42, 0x7C, 0x0 and
> >> 0x0 == 63?????

> >
> > It's a float representation for 63.0
> >

>
> Or more precisely, it's an IEEE 60559 single precision representation
> for 63.0. The code below produces the output "63", and is based on the
> description here:
>
> http://www.psc.edu/general/software/...ieee/ieee.html
>
> The code is not optimised or well tested.
>
> For systems that actually use IEEE single floats, bear in mind that
> they might have a different byte order.
>
> Phil T
>
>
>
> #include <stdio.h>
> #include <math.h>
> #include <float.h>
> #include <assert.h>
>
> double ieee_single(const void *v)
> {
> const unsigned char *data = v;
> int s, e;
> unsigned long src;
> long f;
> double value;
>
> src = ((unsigned long)data[0] << 24) +
> ((unsigned long)data[1] << 16) +
> ((unsigned long)data[2] << +
> ((unsigned long)data[3]);
>
> s = (src & 0x80000000UL) >> 31;
> e = (src & 0x7F800000UL) >> 23;
> f = (src & 0x007FFFFFUL);
>
> if (e == 255 && f != 0) {
> /* NaN - Not a number */
> value = DBL_MAX;
> }
> else if (e == 255 && f == 0 && s == 1) {
> /* Negative infinity */
> value = -DBL_MAX;
> }
> else if (e == 255 && f == 0 && s == 0) {
> /* Positive infinity */
> value = DBL_MAX;
> }
> else if (e > 0 && e < 255) {
> /* Normal number */
> f += 0x00800000UL;
> if (s) f = -f;
> value = ldexp(f, e - 127 - 23);
> }
> else if (e == 0 && f != 0) {
> /* Denormal number */
> if (s) f = -f;
> value = ldexp(f, -126 - 23);
> }
> else if (e == 0 && f == 0 && s == 1) {
> /* Negative zero */
> value = 0;
> }
> else if (e == 0 && f == 0 && s == 0) {
> /* Positive zero */
> value = 0;
> }
> else {
> /* Never happens */
> printf("s = %d, e = %d, f = %lu\n", s, e, f);
> assert(!"Oops, unhandled case in ieee_single()");
> }
>
> return value;
> }
>
> int main(void)
> {
> float f;
> unsigned char combo[] = {0x42, 0x7c, 0x00, 0x00};
> printf("%g\n", ieee_single(combo));
> return 0;
> }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing numbers in binary file eskandari Python 5 05-31-2010 10:39 PM
writing binary file (ios::binary) Ron Eggler C++ 9 04-28-2008 08:20 AM
Finding 1000 largest numbers from a file having some billion numbers Subra C Programming 25 03-08-2007 01:31 AM
Writing Numbers to a Binary File rob C++ 5 02-16-2004 02:15 AM
Binary file - numbers E-Star C++ 13 07-16-2003 09:26 PM



Advertisments