
Toughie: How to convert a binary floating point nu
I am parsing a binary file using ASP, so far I'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't fixed :)<BR><BR>What I have is a series of 4byte 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've now been stuck on the 4byte binary floatingpoint to decimal conversion longer than *any* other programming task I've undertaken! Note that it MUST be done in ASP/VBS  if I had the luxury of using an ActiveX control I wouldn't be posting here :)<BR><BR><BR>Any genius solutions?<BR>

Please show a copy of your code.

Not that tough...
If you can convert integers, then it'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'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't use the same number of exponent bits. And of course you can't represent all the mantissa bits in anything smaller than a double.<BR><BR><BR><BR>

RE: Not that tough...
can you show any of the math req'd to do it?

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..

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/IEEE754hex32.html

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

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 >> 2.0 decimal!<BR><BR>Indeed, there is an *understood* "1" bit to the left of bits 22 to 0. And then the exponent >> 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><%<BR>bytes = Array(3) ' the 4 bytes of the number are put in this array<BR>' it is your responsibility to get them in the right order!<BR>' they might be reversed in the stream!<BR><BR>sign = ASC(bytes(0)) AND &h0080 ' nonzero if negative<BR>exponent = ASC(bytes(0)) AND &h007F ' 7 bits<BR>exponent = exponent * 2 + ASC(bytes(1)) AND &h0080 ' and the 8th bit!<BR>exponent = exponent  127 ' subtract the bias<BR><BR>mantissa = &h0080 + ( ASC(bytes(1) AND &h007F ) ' upper byte of mantissa<BR>mantissa = mantissa * 256 + ASC(bytes(2))<BR>mantissa = mantissa * 256 + ASC(bytes(3)) ' all 3 bytes<BR><BR>result = CDBL(mantissa) / &h800000 ' remember, the binary point is right after the understood one bit<BR><BR>While exponent > 0<BR> result = result * 2<BR> exponent = exponent  1<BR>Wend<BR>While exponent < 0<BR> result = result / 2<BR> exponent = exponent + 1<BR>Wend<BR><BR>%><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

Forum Rules

