[Gambas-user] Hex string to float

Ron_1st ronstk at ...239...
Fri Dec 12 19:42:09 CET 2008


On Friday 12 December 2008, Ron wrote:
> Ron schreef:
> >
> > Hi,
> >
> > Anyone have a routine to convert a hex string to a float?
> > Like these:
> > B6FBEB3A = -0.000007507766895287204
> > 3C5D2104 = 0.01349664106965065
> >
> > Find lots of vb code, but those use copymem functions.
> >
> > Thanks in advance!
> >
> To answer my own question:
> 
> I ended up with this so far:
> 
> PUBLIC SUB Main()
> 
>   PRINT HexToFloat("3C5D2104")
>   PRINT HexToFloat("B6FBEB3A")
> 
> END
> 
> PUBLIC FUNCTION HexToFloat(sHex AS String) AS Float
> 
>   DIM sTemp AS String
>   DIM iSign, iExponent AS Integer
>   DIM fTemp, fMant, fResult AS Float
> 
>   PRINT "Value = " & sHex
>   ' calculate sign
>   sTemp = Mid(sHex, 1, 2)
>   fTemp = Val("&H" & sTemp) AND &H80
>   iSign = IIf(fTemp = 128, -1, 1)
>   PRINT "Sign = " & iSign
> 
>   ' calculate exponent
>   sTemp = Mid(sHex, 1, 3)
>   fTemp = Val("&H" & sTemp) AND &H7F8
>   iExponent = fTemp / 2 ^ 3 - 127
>   PRINT "Exponent = " & iExponent
> 
>   ' calculate mantissa
>   sTemp = Mid(sHex, 3, 6)
>   fTemp = Val("&H" & sTemp) AND &H7FFFFF
>   fMant = (fTemp / 2 ^ 23) + 1
>   PRINT "Mantissa = " & fMant
> 
>   fResult = iSign * fMant * 2 ^ iExponent
> 
>   RETURN fResult
> 
> END
> 
> Value = 3C5D2104
> Sign = 1
> Exponent = -7
> Mantissa = 1.727570056915
> 
> Result: 0.01349664107
> 
> Value = B6FBEB3A
> Sign = -1
> Exponent = -18
> Mantissa = 1.968116044998
> 
> Result: -7.507766895287E-6
> 
> Regards,
> Ron_2nd
> 
> 

Hi Ron :)
It's a very long time ago I had to manage same 'problem'

First you need to know what notation is in the hex number.
I only know a little of two of them and second you cant do
strict hex to float. Its binary written in short by hex digits.

1) The left most bit is the sign of the number and every bit is 1/2
  01001001 is positive 1*0.5 + 0*.25 + 0*.125 + 1*0.062 + 0*0.031 +0*0.016 +1*0.008
in hex 0x49

2) the leftmost is the sign, then 8 bits for the exponent and followed
23 bits for the mantisa. (it looks to me you have/use this notation)

The first step is to convert the hex number to binary.
Seperate the sign bit from the bits and loop the remaining bits 
is the base method in both notations.
The bad part is the fractional part but if you have a fixed count of bits 
you can first multiply by a fix number.
i.e. if you have 8 bits (1 sign 7 number) you multiply by 128, 
for 16 bits (1 sign,15 number) you multiply by 65536.
After dont the calculations you know you need to divide by that number.

I did it in Z80 machine language and the method was good enough, using 
a quarter of a sinus table (180 steps of 0.5 grad per byte) and usage 
of pitagoras to draw circles on the screen.
Her the multiply was done by shift left, divide by shift right.
The table was the sinus table value for 90 degree multiplied by 256

If floppy disks have a lifetime of centuries the I maybe can find it back
on a real 5.25 floppy disk :)

links:
google:floating number in binary
IEEE 754-2008 - Wikipedia, the free encyclopedia
  http://en.wikipedia.org/wiki/IEEE_754
IEEE Standard 754 Floating-Point
  http://steve.hollasch.net/cgindex/coding/ieeefloat.html
Binary floating point and .NET
  http://www.yoda.arachsys.com/csharp/floatingpoint.html
  Read the part:
    What exactly does a floating point number look like in memory?

IEEE Floating-Point Binary Representation
  http://hankfiles.pcvsconsole.com/answer.php?file=454

So the keyword is 'IEEE754' for search.

google:floating number in binary  z80
(nice results)
Understanding Floating Point Formats
  http://aplawrence.com/Basics/floatingpoint.html (perl script)




Best regards
Ron_1st






More information about the User mailing list