Toughie: How to convert a binary floating point nu

# Thread: Toughie: How to convert a binary floating point nu

1. Junior Member
Join Date
Dec 1969
Posts
3

## Toughie: How to convert a binary floating point nu

I am parsing a binary file using ASP, so far I&#039;ve managed to succesfully retrieve & convert hex, text and embedded binary files no problem, but have now hot a stumbling block that even 3 cups of coffee haven&#039;t fixed :)<BR><BR>What I have is a series of 4-byte floating point numbers that I need to convert to decimal. I am currently stepping through the binary data using ADODB.Stream and coverting the binary data I need to capture to ASCII as I go..<BR><BR>I&#039;ve now been stuck on the 4-byte binary floating-point to decimal conversion longer than *any* other programming task I&#039;ve undertaken! Note that it MUST be done in ASP/VBS - if I had the luxury of using an ActiveX control I wouldn&#039;t be posting here :)<BR><BR><BR>Any genius solutions?<BR>

2. Senior Member
Join Date
Dec 1969
Posts
3,195

.

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Not that tough...

If you can convert integers, then it&#039;s not that bad.<BR><BR>Been *YEARS* since I did this, but I remember the concepts.<BR><BR>You have to figure out which bits represent the exponent and the mantissa. I&#039;m pretty sure the mantissa has an *understood* 1 bit preceding it. The exponent is "biased". I *think* it is 7 bits, biased by 0x40, meaning that an exponent of 0x40 is actually an exponent of zero. 0x3F would be an exponent of -1, etc.<BR><BR>So you convert the mantissa bits to an integer, remembering that "understood" bit. Then convert the exponent. And then remember that the exponent is a power of two, not 10. So just multiply or divide by 2 as needed. Finally, apply the sign bit.<BR><BR>The fun part is that 8 bit floating point doesn&#039;t use the same number of exponent bits. And of course you can&#039;t represent all the mantissa bits in anything smaller than a double.<BR><BR><BR><BR>

4. Junior Member
Join Date
Dec 1969
Posts
3

## RE: Not that tough...

can you show any of the math req&#039;d to do it?

5. Junior Member
Join Date
Dec 1969
Posts
3

## RE: Toughie: How to convert a binary floating poin

To reiterate:<BR><BR>I have a 32bit floating point number in *binary* (not ASCII) that I need to convert to a deimcal number that vbs/asp can use..

6. Senior Member
Join Date
Dec 1969
Posts
3,195

## This may help

View the source on the following and take a look at the javascript functions for conversion:<BR><BR>http://babbage.cs.qc.edu/courses/cs341/IEEE-754hex32.html

7. Senior Member
Join Date
Dec 1969
Posts
3,195

## Also

http://sandbox.mc.edu/~bennet/cs110/flt/ftod.html<BR><BR>Again hope that helps<BR>Pete

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Wonderful site!

You will notice how *close* I was! The bias is 128, not 64, but it *looks* like 0x40 (64) because it is shifted one bit right, to make room for the sign bit. Well, not quite. I was off by 1. 127 is the "zero" point of the bias. <BR><BR>Enter 40000000 as the hex number to convert and see what you get!<BR><BR>0x40000000 --&#062;&#062; 2.0 decimal!<BR><BR>Indeed, there is an *understood* "1" bit to the left of bits 22 to 0. And then the exponent --&#062;&#062; 10000000, 128. And that is 1 more than the "root" value of 127, so that means multiply by 2^1 power, which is of course 2, so 1 times 2 is 2.0 decimal.<BR><BR>Whew! Amazing both how close I remembered this after 10 years not using it and also how much I blew it. Tch.<BR><BR>If you can do binary integers, you can do this one!<BR><BR>Here is a stab, 100% UNTESTED:<BR><BR>&#060;%<BR>bytes = Array(3) &#039; the 4 bytes of the number are put in this array<BR>&#039; it is your responsibility to get them in the right order!<BR>&#039; they might be reversed in the stream!<BR><BR>sign = ASC(bytes(0)) AND &h0080 &#039; non-zero if negative<BR>exponent = ASC(bytes(0)) AND &h007F &#039; 7 bits<BR>exponent = exponent * 2 + ASC(bytes(1)) AND &h0080 &#039; and the 8th bit!<BR>exponent = exponent - 127 &#039; subtract the bias<BR><BR>mantissa = &h0080 + ( ASC(bytes(1) AND &h007F ) &#039; upper byte of mantissa<BR>mantissa = mantissa * 256 + ASC(bytes(2))<BR>mantissa = mantissa * 256 + ASC(bytes(3)) &#039; all 3 bytes<BR><BR>result = CDBL(mantissa) / &h800000 &#039; remember, the binary point is right after the understood one bit<BR><BR>While exponent &#062; 0<BR>&nbsp; &nbsp; result = result * 2<BR>&nbsp; &nbsp; exponent = exponent - 1<BR>Wend<BR>While exponent &#060; 0<BR>&nbsp; &nbsp; result = result / 2<BR>&nbsp; &nbsp; exponent = exponent + 1<BR>Wend<BR><BR>%&#062;<BR><BR>Does that work????<BR><BR>Sorry no time to test today!<BR><BR><BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•