IEEE 754 Conversion - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

IEEE 754 Conversion

Is there some source code for converting
numbers to IEEE 754 32 Bit Hex format?


  • bobalongbobalong Posts: 3Member
    : Is there some source code for converting
    : numbers to IEEE 754 32 Bit Hex format?

    what you need is a union, but this code does it the hard way - enjoy!
    Copy and paste into your app. The wrapped around lines should sort themselves out.

    Function dword_to_float(ho As Long, lo As Long) As Single
    'NOTE ho and lo are the 2 16 bit halves of the float
    'held in longs so that be unsigned (range from 0 to 65535)
    'IMPORTANT - YOU MUST! PUT a & on the end of each argument when you call this with an &H arg !!!!!!!!!!!!!!
    'author: [email protected] -- [email protected] UK -
    'float format:
    'bit0 sign
    'next 8bits are exp+127
    'rest is 23bit mantissa with assumed binary 1. in front (making 24bits)

    '1 get exp
    '2 sub 127
    '3 get mantissa 23bits
    '4 insert implied binary 1. at start giving 24bits
    '5mult by 2^exp
    'negate if sign bit set

    Dim negative As Integer
    Dim sign, hi_mantissa, lo_mantissa, exponent, result As Double
    negative = ((ho And &HFFFF8000) <> 0) 'mask out exp and mantissa leaving only sign
    exponent = ((ho And &H7F80) 128) - 127 'mask out sign, shr 7 places, sub 127 giving exp
    If exponent > 127 Then exponent = 127
    hi_mantissa = CDbl((ho And &H7F&) Or &H80&) / 128# 'mask out exp & sign, set implied msb of mantisa, scale to 1.xxxxxx binary
    lo_mantissa = (CDbl(lo) / (128# * 65536#)) 'scale to 0.00000000xxxxxxxxxxxxxxxx
    If negative Then sign = -1 Else sign = 1
    'Debug.Print "hm"; hi_mantissa; "lm"; lo_mantissa;
    result = sign * (hi_mantissa + lo_mantissa) * 2 ^ exponent
    dword_to_float = CSng(result)
    End Function
    'float_to_dword f,h,l sets h & l to the 2 16bit halves of the 32bit float f
    'where h is the high order half and l the low order half
    Sub float_to_dword(ByVal f As Single, ho As Long, lo As Long)
    'NOTE ho and lo hold the 2 16bit halves of the 32bit float
    'they are longs so that they can be UNSIGNED. In other words they
    'range from 0 to 65535
    'author [email protected] -- [email protected] UK -
    '1 find sign
    '2 find exp
    '3 find mantissa
    '4 ho msbit=sign
    '5 ho next 8bits = exp
    '6 ho next 7bits = high 7 bits of mantissa
    '7 lo = low 16bits of mantissa
    If f = 0 Then 'alas, 0 is a special case
    ho = 0&
    lo = 0&
    Dim sign As Long, exponent As Long, mantissa As Double, d As Double
    '1 sign
    If f < 0 Then
    sign = &H8000&
    d = -f
    sign = 0&
    d = f
    End If
    '2 exp
    'f is a mantissa in the range 1.0000000000 to 1.99999999999 x 2^(exponent_field-127)
    'so keep dividing/multing float by 2 until in the range 1.0 to 1.999..
    'while counting divs/mults. This count is the true exponent (pwr of 2)
    'to which we add 127 giving the exponent_field
    exponent = 127
    If d >= 2 Then
    While d >= 2# 'd was originally a copy of f
    d = d / 2# 'd ends up as the 24bit mantissa
    exponent = exponent + 1 'exponent ends up as the 8bit exponent
    While d < 1#
    d = d * 2#
    exponent = exponent - 1
    End If
    '3 24bit mantissa is in d, remove the msbit since it's always 1, giving a 23bit fractional mantissa field
    d = d - 1# 'd is now 0 to .99999999 (meaning 1.0.. to 1.999..). The 1. is ASSUMED!
    '4,5,6 ho = sign OR exp+127 OR high 7 bits of mantissa
    ho = CLng(sign + (exponent * 128&) + Int(d * 128#))

    '7 lo = low 16 bits of mantissa
    lo = CLng(d * 8388608#) And &HFFFF& 'x 8388608 magnifies 23bit fractional mantissa into a 23bit long
    End If

    End Sub

Sign In or Register to comment.