1

In VB6, I am trying to convert a number to binary but when the number has 10 digits i am always getting an Overflow error. What is the data type where i can store a trillion number? This is the code which is working when the number has less that 10 digits.

Public Function DecimalToBinary(DecimalNum As Double) As _
String
Dim tmp As String
Dim n As Double

n = DecimalNum

tmp = Trim(Str(n Mod 2))
n = n \ 2

Do While n <> 0
    tmp = Trim(Str(n Mod 2)) & tmp
    n = n \ 2
Loop

DecimalToBinary = tmp
End Function
Ruby Kousinovali
  • 337
  • 2
  • 20
  • 1
    `What is the data type where i can store a trillion number?` - https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/data-type-summary – GSerg Sep 11 '20 at 21:45
  • Instead of send me the link to datatypes, can you tell which of them is the one i need, because the only i see can store this number is the LongLong which is not supported by VB6 – Ruby Kousinovali Sep 12 '20 at 19:43

1 Answers1

3

One of the problems you will encounter is that the Mod operator will not work with values larger than a Long (2,147,483,647). You can rewrite a Mod function as described in this answer: VBA equivalent to Excel's mod function:

' Divide the number by 2.
' Get the integer quotient for the next iteration.
' Get the remainder for the binary digit.
' Repeat the steps until the quotient is equal to 0.
Public Function DecimalToBinary(DecimalNum As Double) As String
    Dim tmp As String
    Dim n As Double
    
    n = DecimalNum
    
    Do While n <> 0
        tmp = Remainder(n, 2) & tmp
        n = Int(n / 2)
    Loop
    
    DecimalToBinary = tmp

End Function

Function Remainder(Dividend As Variant, Divisor As Variant) As Variant
    Remainder = Dividend - Divisor * Int(Dividend / Divisor)
End Function

You can also rewrite your function to avoid Mod altogether:

Public Function DecimalToBinary2(DecimalNum As Double) As String
    Dim tmp As String
    Dim n As Double
    Dim iCounter As Integer
    Dim iBits As Integer
    Dim dblMaxSize As Double
    
    n = DecimalNum
    iBits = 1
    dblMaxSize = 1
    
    ' Get number of bits
    Do While dblMaxSize <= n
        dblMaxSize = dblMaxSize * 2
        iBits = iBits + 1
    Loop

    ' Move back down one bit
    dblMaxSize = dblMaxSize / 2
    iBits = iBits - 1

    ' Work back down bit by bit
    For iCounter = iBits To 1 Step -1
        If n - dblMaxSize >= 0 Then
            tmp = tmp & "1"
            n = n - dblMaxSize
        Else
            ' This bit is too large
            tmp = tmp & "0"
        End If
        dblMaxSize = dblMaxSize / 2
    Next
    
    DecimalToBinary2 = tmp

End Function

This function finds the bit that is larger than your number and works back down, bit by bit, figuring out if the value for each bit can be subtracted from your number. It's a pretty basic approach but it does the job.

For both functions, if you want to have your binary string in groups of 8 bits, you can use a function like this to pad your string:

Public Function ConvertToBytes(p_sBits As String)
    Dim iLength As Integer
    Dim iBytes As Integer
    
    iLength = Len(p_sBits)
    If iLength Mod 8 > 0 Then
        iBytes = Int(iLength / 8) + 1
    Else
        iBytes = Int(iLength / 8)
    End If
    
    ConvertToBytes = Right("00000000" & p_sBits, iBytes * 8)

End Function
Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29
  • Which implementation are you referring to? – Étienne Laneville Sep 13 '20 at 20:14
  • Both functions return the same string. I've tested both with random values and compared them with [Decimal to Binary Converter](https://www.binaryhexconverter.com/decimal-to-binary-converter) output and they match. – Étienne Laneville Sep 13 '20 at 20:39
  • The DecimalToBinary2 is return wrong number. I tried 3252027161 and returns 011000001110101011111111100011001 and the correct is 1100 0001 1101 0101 1111 1111 0001 1001 (one zero in front) and 436196801 which returns 011001111111111101010111000001 and the correct is 1 1001 1111 1111 1101 0101 1100 0001 – Ruby Kousinovali Sep 13 '20 at 20:54
  • 2
    Note that this will fail for integers larger than `9,007,199,254,740,990` due to the limited precission of floating point numbers. Not a problem for 10 digit numbers, but may be of interest to future readers – chris neilsen Sep 13 '20 at 22:20
  • Thank you @ÉtienneLaneville – Ruby Kousinovali Sep 18 '20 at 21:38