CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2000
    Posts
    1

    Convert Hexadecimal to Floating Point

    Call me an idiot, but for the life in me i cannot figure out how to do a simple conversion......
    I need to convert
    FB7E0E41 to 8.9060
    32 bit IEEE Hex to decimal
    can ANYONE Help ME!!!!!

    Thanks
    Nick Stone



  2. #2
    Join Date
    Mar 2000
    Posts
    95

    Re: Convert Hexadecimal to Floating Point

    You're not an idiot and and it's not a simple conversion. As far as I know there is no available ieee hex to float functions for vb.

    Here's how you break down the hex value.
    You're going to have to do a search for more info
    because this might get confusing.

    FB7E0E41 (hex)
    1111 1011 0111 1110 0000 1110 0100 0001 (binary)

    Reading the bits left to right....

    The Sign of the mantissa (and therefore the number) is 1 (MSB) which represents a negative value.

    The Exponent is 11110110 (binary) or 246 decimal.
    The exponent field is represented in excess 127 so the exponent value is 119

    The Mantissa is 11111100000111001000001 binary
    with the implied leading 1, the mantissa is (1).FC1C82 (hex)

    to get the floating point representation....

    1*2^0 /* 1 */
    + 1*2^-1 + 1*2^-2 + 1*2^-3 + 1*2^-4 /* F */
    + 1*2^-5 + 1*2^-6 + 0*2^-7 + 0*2^-8 /* C */
    + 0*2^-9 + 0*2^-10 + 0*2^-11 + 1*2^-12 /* 1 */
    + 1*2^-13 + 1*2-14 + 0*2^-15 + 0*2^-16 /* C */

    etcetera...

    The floating point representation is therefore -1.98480999 times 2 to the 119

    Decimal equivalent: -1.31913E+036

    I don't know where you got the 8.906 value.

    Curt




  3. #3
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Convert Hexadecimal to Floating Point

    I'm not familiar with the IEEE thing or the exact specification of floating point, but here are two functions to convert any number from decimal to any base and back again. Using these i was able to convert the original number in base 16 to base 10 and then to the binary number that Curt came up with. So, I think these might help:

    public Function ConvertDecToBaseN(byval dValue as Double, optional byval byBase as Byte = 16) as string
    Const BASENUMBERS as string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim sResult as string
    Dim dRemainder as Double

    on error GoTo ErrorHandler

    sResult = ""

    If (byBase < 2) Or (byBase > 36) then GoTo Done

    dValue = Abs(dValue)

    Do
    dRemainder = dValue - (byBase * Int((dValue / byBase)))
    sResult = mid$(BASENUMBERS, dRemainder + 1, 1) & sResult
    dValue = Int(dValue / byBase)

    Loop While (dValue > 0)

    Done:
    ConvertDecToBaseN = sResult
    Exit Function

    ErrorHandler:
    Err.Raise Err.Number, "ConvertDecToBaseN", Err.Description
    End Function

    public Function ConvertBaseNToDec(byval dValue as string, optional byval byBase as Byte = 16) as string
    Const BASENUMBERS as string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim sResult as string
    Dim lReturn as Double
    Dim n as Integer

    on error GoTo Handler

    If (byBase < 2) Or (byBase > 36) then Exit Function

    n = 0

    Do
    lReturn = ((InStr(1, BASENUMBERS, mid(dValue, (len(dValue) - n), 1)) - 1) * (byBase ^ n)) + lReturn
    n = n + 1
    Loop Until n = len(dValue)

    ConvertBaseNToDec = CStr(lReturn)

    Exit Function

    Handler:
    Err.Raise Err.Number, "ConvertBaseNToDec", Err.Description
    End Function




    You could probably modify these to go directly from any base to any other base, but I haven't had time to do that yet.

    Also, these dont seem to handle rational or irrational numbers (non-whole numbers), sorry.

    hope this helps,

    John

    John Pirkey
    MCSD
    http://www.ShallowWaterSystems.com
    http://www.stlvbug.org
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  4. #4
    Join Date
    Feb 2001
    Location
    New Jersey
    Posts
    312
    These two things just dont seem to work

    <


    >
    http://www.dewgames.com
    Shareware and Free games!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured