Search code examples
vb6

Convert Trillion number to Binary


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

Solution

  • 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