vbScript - Convert Integer to Binary String

Please see my post vbScript - The Basics for more details on vbScript.

vbScript has a number of functions for converting from one type to another. These functions are named C<type> as in CInt (convert to int), CDbl (convert to double), etc. There are also functions that will format numbers as string (FormatNumber, FormatPercent, FormatCurrentcy, Hex, etc.) One glaring omission is a function to format numbers (integers) as binary. Fortunately the process is straightforward.

If we first convert the number to e hexadecimal string we can then convert each hex digit into its equivalent four digit binary string. By finding the index (one relative) of the hex digit in the string 0123456789ABCDEF and subtracting one, we get a zero relative number which we can then use as an index into an array that maps each hex digit to its four digit binary equivalent.

Since long binary strings are difficult to interpret, let's also provide a grouping function that will return the binary string in user specified groups. For example, Group(str,4) will return the string in groups of four characters separated by a space. The two functions are given here along with a short piece of test code. Note that Group will work on any string, not just binary strings.

''#region Header                                                                        '
''                                                                                      '
''  Name:                                                                               '
''                                                                                      '
''      FormatBinary.vbs                                                                '
''                                                                                      '
''  Description:                                                                        '
''                                                                                      '
''      Given an integer value, FormatBinary returns a string of the given width of the '
''      binary representation of that number. Group returns a string with the chars in  '
''      the original string arranged in groups of the given width.                      '
''                                                                                      '
''  Example:                                                                            '
''                                                                                      '
''      FormatBinary(655321,32)                                                         '
''      00000000000010011111111111011001                                                '
''                                                                                      '
''      Group(FormatBinary(655321),4)                                                   '
''      0000 0000 0000 1001 1111 1111 1101 1001                                         '
''                                                                                      '
''  Audit:                                                                              '
''                                                                                      '
''      2017-06-01  rj  original code                                                   '
''                                                                                      '
''#endregion                                                                            '

Function FormatBinary (ByVal num, width)

    Dim h: h = UCase(Hex(num))
    Dim p: p = "0123456789ABCDEF"
    Dim b: b = Array("0000","0001","0010","0011","0100","0101","0110","0111", _
                     "1000","1001","1010","1011","1100","1101","1110","1111")

    FormatBinary = ""

    Dim i: For i = 1 To Len(h)
        FormatBinary = FormatBinary & b(instr(p,Mid(h,i,1))-1)
    Next

    'Pad on left to requested width (max 64)

    Dim pad: If num < 0 Then pad = "1": Else pad = "0": End If
    FormatBinary = Right(Replace(Space(64)," ",pad) & FormatBinary,width)

End Function

''Format string into groups of a given width                                            '
Function Group (ByVal str, width)

    Group = ""

    Do While Len(str) > 0
        Group = Group & " " & Left(str,width)
        str = Mid(str,width+1)
    Loop

    'Return everything but the leading space

    Group = Mid(Group,2)

End Function

''Test code                                                                             '
If StrComp(Wscript.ScriptName,"FormatBinary.vbs",vbTextCompare) = 0 Then
    i = -655321
    Wscript.Echo i,vbtab, "Hex=". & Hex(i) & " Bin=   " & FormatBinary(i,32)
    Wscript.Echo i,vbtab, "Hex=" & Hex(i) & " Group= " & Group(FormatBinary(i,64),4)
End If