I recently felt the need too display some numbers in binary, and after getting sick of working it out on paper, this function was born (I completely forgot you can use the Windows calculator ) And just now I added the facility to display the binary output of strings as well.

To call it just pass either a string or number and optionally a fixed length for the output

VB Code:
  1. Public Function DumpBits(ByRef pValue As Variant, Optional ByVal lLenDesired As Long = -1) As String
  2. Dim chBuffer()  As Byte
  3. Dim i           As Long
  4. Dim j           As Long
  5.  
  6.     If (VarType(pValue) = vbString) Then
  7.         If (lLenDesired < 1) Then lLenDesired = Len(pValue) * 2
  8.  
  9.         ReDim chBuffer(lLenDesired)
  10.         chBuffer = pValue
  11.  
  12.         For i = UBound(chBuffer) To 0 Step -1
  13.             For j = 1 To 8
  14.                 DumpBits = CStr(Abs((chBuffer(i) And (2 ^ (j - 1))) > 0)) & DumpBits
  15.             Next j
  16.         Next i
  17.  
  18.       Else
  19.         If (lLenDesired < 1) Then
  20.             lLenDesired = 0
  21.             Do While (pValue \ (2 ^ lLenDesired)) > 0
  22.                 lLenDesired = lLenDesired + 1
  23.             Loop
  24.         End If
  25.  
  26.         For i = 1 To lLenDesired
  27.             DumpBits = CStr(Abs((pValue And (2 ^ (i - 1))) > 0)) & DumpBits
  28.         Next i
  29.  
  30.     End If
  31.  
  32.     If (Len(DumpBits) > lLenDesired) Then
  33.         Do While Left$(DumpBits, 1) = "0" And Len(DumpBits) > lLenDesired
  34.             DumpBits = Right$(DumpBits, Len(DumpBits) - 1)
  35.         Loop
  36.  
  37.       ElseIf (Len(DumpBits) < lLenDesired) Then
  38.         Do While Len(DumpBits) < lLenDesired
  39.             DumpBits = "0" & DumpBits
  40.         Loop
  41.  
  42.     End If
  43. End Function