Results 1 to 5 of 5

Thread: [RESOLVED] VB6 Convert Double to Binary String

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Resolved [RESOLVED] VB6 Convert Double to Binary String

    Can you please help me Convert Double to Binary String?

    for example, -1.75888576432121E+16 to Binary string.

    My code only can deal with Long type.

    Code:
    Public Function LongToBitString(theValue As Long) As String
    
    ' Function converts byte, integer, long to a bit string
    ' String length is equal to bit count of the passed variable
    
    Dim b As Integer, tValue As Long
    Dim mPO2lut(0 To 31) As Double ' 2^31 is a double not Long & used when wrapping shifts
    Const bitOn As String = "1"
    Const bitOff As String = "0"
    
    Const HighBitMask As Long = &H80000000
    Const bitLen As Long = 32
    
    Dim i As Integer
    
        mPO2lut(0) = 1#
        For i = 1 To 31
            mPO2lut(i) = mPO2lut(i - 1) * 2&
        Next i
    
        tValue = (theValue And Not HighBitMask)
        LongToBitString = String$(bitLen, bitOff)
        For b = 0 To bitLen - 2
            If ((tValue \ mPO2lut(b)) And 1) Then Mid$(LongToBitString, bitLen - b, 1) = bitOn
        Next b
        If (theValue And HighBitMask) Then Mid$(LongToBitString, 1, 1) = bitOn
    
    End Function
    Attach VB6 Data Types:
    Long (long integer) 4 bytes -2,147,483,648 to 2,147,483,647
    Single (single-precision floating-point) 4 bytes -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values. About 6 or 7 significant figures accuracy.
    Double (double-precision floating-point) 8 bytes -1.79769313486231E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values. About 15 or 16 significant figures accuracy.
    Currency (scaled integer) 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807
    Decimal 14 bytes +/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is +/-0.0000000000000000000000000001
    Last edited by Jonney; Jul 23rd, 2014 at 08:18 PM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: VB6 Convert Double to Binary String

    Code:
    Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal ByteLen As Long)
    
    Public Function DoubleToBitString(theValue As Double) As String
      Dim LongArray(1) As Long
      CopyMemory LongArray(0), theValue, 8
      DoubleToBitString = LongToBitString(LongArray(1)) & LongToBitString(LongArray(0))
    End Function

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: VB6 Convert Double to Binary String

    Quote Originally Posted by passel View Post
    Code:
    Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal ByteLen As Long)
    
    Public Function DoubleToBitString(theValue As Double) As String
      Dim LongArray(1) As Long
      CopyMemory LongArray(0), theValue, 8
      DoubleToBitString = LongToBitString(LongArray(1)) & LongToBitString(LongArray(0))
    End Function
    doesn't work as I expect.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: VB6 Convert Double to Binary String

    What did you expect?

    Your LongToBitString function returns a 32 character string of 0's and 1's, most significant bit first.

    The DoubleToString simply copies the 8 bytes of a double into two Longs, and then calls your function twice, passing the Most Significant half first and concatenates the lower half to that, so you get a 64 character string of 0's and 1's, most significant bit first.

    For instance, for a double value of 1.0, the 8 bytes in hex are:
    3FF0000000000000
    The function returns the binary string
    0011111111110000000000000000000000000000000000000000000000000000
    which is the same thing as the hex.

    If you have 2.0 in the double the hex value of the 8 bytes are:
    4000000000000000
    and the string returned from the function is
    0100000000000000000000000000000000000000000000000000000000000000
    which is the binary representation of a double with the value of 2.

    I made the assumption that what your supplied function returned, a string of 32 bits represented by a string of 0's and 1's, was what you wanted for a double, of course a double is 64 bits, not 32.

    p.s. Some other examples.
    Since numbers such as 0.1 can't be represented exactly in Floating point (it is an infinite series of repeating binary digits), it will give a more interesting pattern than the Floating point values holding simple integer values, as above.
    0.1 in a double, in Hex
    3FB999999999999A, and in Binary
    0011111110111001100110011001100110011001100110011001100110011010

    In Hex, it is obvious that the 9999.... would go on forever, but since you only have 64 bits, the last bit is rounded up, so the last Hex digit is A, instead of 9.

    A few examples of numbers that are powers of 2, stored in a double, and what the resulting bit pattern is, in Hex
    2.0 = 400000000 = 1 * 2^1
    4.0 = 401000000 = 1 * 2^2
    8.0 = 402000000 = 1 * 2^3
    16.0 = 403000000 = 1 * 2^4
    32.0 = 404000000 = 1 * 2^5

    What you are seeing above is the incrementing of the exponent portion of the floating point number.
    Since the most significant bit of the mantissa is always 1, the 1 is not actually stored, to give an extra bit of precision in the floating point format, so you won't see that "1" in the above numbers since they are all integer powers of 2, and integer powers of two would have only 1 bit set,i.e, as an integer type, the first few powers of two in binary are:
    1 = 1
    2 = 10
    4 = 100
    8 = 1000
    16 = 10000
    32 = 100000

    That MSB "1", will not be part of the floating point number, it is "implied" to exist as one additional bit "above" the most significant bit of the mantissa portion of the floating point number.
    (I'm describing the IEEE standard representation of Floating Point, which most machines use these days, but there are other representations and those may or may not have an implied "1" as the MSB of the mantissa.)

    A couple of example of integer numbers that are not powers of two. Both 3 and 5 would have a second bit set, i.e. in binary:
    3 = 11
    5 = 101

    You would see that second bit in the floating point representation, i.e. in hex:
    3 = 400800000
    5 = 401400000

    Refer back to the hex double representations above of 2 and 4, to see that 3 and 5 have one additional bit set.

    This may still be unclear to you, but I don't intend to beat any more on this subject since you can get more detail on floating point representation by searching for information on the internet.

    If this function is not what you want, then you will have to be more specific with examples of what you expect the function to return, as I have no idea if it is not what it currently is doing.
    Last edited by passel; Jul 24th, 2014 at 09:45 AM. Reason: More examples of Double Floating point formats

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: VB6 Convert Double to Binary String

    Quote Originally Posted by passel View Post
    What did you expect?

    Your LongToBitString function returns a 32 character string of 0's and 1's, most significant bit first.

    The DoubleToString simply copies the 8 bytes of a double into two Longs, and then calls your function twice, passing the Most Significant half first and concatenates the lower half to that, so you get a 64 character string of 0's and 1's, most significant bit first.

    For instance, for a double value of 1.0, the 8 bytes in hex are:
    3FF0000000000000
    The function returns the binary string
    0011111111110000000000000000000000000000000000000000000000000000
    which is the same thing as the hex.

    If you have 2.0 in the double the hex value of the 8 bytes are:
    4000000000000000
    and the string returned from the function is
    0100000000000000000000000000000000000000000000000000000000000000
    which is the binary representation of a double with the value of 2.

    I made the assumption that what your supplied function returned, a string of 32 bits represented by a string of 0's and 1's, was what you wanted for a double, of course a double is 64 bits, not 32.

    p.s. Some other examples.
    Since numbers such as 0.1 can't be represented exactly in Floating point (it is an infinite series of repeating binary digits), it will give a more interesting pattern than the Floating point values holding simple integer values, as above.
    0.1 in a double, in Hex
    3FB999999999999A, and in Binary
    0011111110111001100110011001100110011001100110011001100110011010

    In Hex, it is obvious that the 9999.... would go on forever, but since you only have 64 bits, the last bit is rounded up, so the last Hex digit is A, instead of 9.

    A few examples of numbers that are powers of 2, stored in a double, and what the resulting bit pattern is, in Hex
    2.0 = 400000000 = 1 * 2^1
    4.0 = 401000000 = 1 * 2^2
    8.0 = 402000000 = 1 * 2^3
    16.0 = 403000000 = 1 * 2^4
    32.0 = 404000000 = 1 * 2^5

    What you are seeing above is the incrementing of the exponent portion of the floating point number.
    Since the most significant bit of the mantissa is always 1, the 1 is not actually stored, to give an extra bit of precision in the floating point format, so you won't see that "1" in the above numbers since they are all integer powers of 2, and integer powers of two would have only 1 bit set,i.e, as an integer type, the first few powers of two in binary are:
    1 = 1
    2 = 10
    4 = 100
    8 = 1000
    16 = 10000
    32 = 100000

    That MSB "1", will not be part of the floating point number, it is "implied" to exist as one additional bit "above" the most significant bit of the mantissa portion of the floating point number.
    (I'm describing the IEEE standard representation of Floating Point, which most machines use these days, but there are other representations and those may or may not have an implied "1" as the MSB of the mantissa.)

    A couple of example of integer numbers that are not powers of two. Both 3 and 5 would have a second bit set, i.e. in binary:
    3 = 11
    5 = 101

    You would see that second bit in the floating point representation, i.e. in hex:
    3 = 400800000
    5 = 401400000

    Refer back to the hex double representations above of 2 and 4, to see that 3 and 5 have one additional bit set.

    This may still be unclear to you, but I don't intend to beat any more on this subject since you can get more detail on floating point representation by searching for information on the internet.

    If this function is not what you want, then you will have to be more specific with examples of what you expect the function to return, as I have no idea if it is not what it currently is doing.
    Sorry for misleading you.
    What I need is to convert C# int to VB's binary data.
    I didn't notice C# int is the same with VB's Long in Signed 32-bit length. I used double type array to hold c#'s int array MISTAKELY, so it lead me a wrong question.

    OK,I have to close my thread. I am highly appreciated for your "DOUBLE to binary" codes.

    Thank you,Sir.

    Name:  csharpint.jpg
Views: 3278
Size:  31.1 KB
    Last edited by Jonney; Jul 24th, 2014 at 10:21 AM.

Tags for this Thread

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