Task

Create and display the sequence of binary digits for a given   non-negative integer.

   The decimal value      5   should produce an output of               101
   The decimal value     50   should produce an output of            110010
   The decimal value   9000   should produce an output of    10001100101000

The results can be achieved using built-in radix functions within the language   (if these are available),   or alternatively a user defined function can be used.

The output produced should consist just of the binary digits of each number followed by a   newline.

There should be no other whitespace, radix or sign markers in the produced output, and leading zeros should not appear in the results.




 
Option Explicit
 
Sub Main_Dec2bin()
Dim Nb As Long
Nb = 5
    Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin(Nb)
    Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin2(Nb)
Nb = 50
    Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin(Nb)
    Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin2(Nb)
Nb = 9000
    Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin(Nb)
    Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin2(Nb)
End Sub
 
Function DecToBin(ByVal Number As Long) As String
Dim strTemp As String
 
    Do While Number > 1
        strTemp = Number - 2 * (Number \ 2) & strTemp
        Number = Number \ 2
    Loop
    DecToBin = Number & strTemp
End Function
 
Function DecToBin2(ByVal Number As Long, Optional Places As Long) As String
    If Number > 511 Then
        DecToBin2 = "Error : Number is too large ! (Number must be < 511)"
    ElseIf Number < -512 Then
        DecToBin2 = "Error : Number is too small ! (Number must be > -512)"
    Else
        If Places = 0 Then
            DecToBin2 = WorksheetFunction.Dec2Bin(Number)
        Else
            DecToBin2 = WorksheetFunction.Dec2Bin(Number, Places)
        End If
    End If
End Function
 