Option Explicit
'Function by Rob Bovey
Function vDecimalToBinary( _
ByVal lDecimal As Long, _
Optional ByVal bReturnArray As Boolean = False, _
Optional ByVal b32Bit As Boolean = False) As Variant
Const l16_BITS As Long = 15
Const l32_BITS As Long = 31
Dim dPlaceArray() As Double
Dim lCount As Long
Dim lNumBits As Long
Dim lReturn() As Long
Dim szReturn As String
If b32Bit Then lNumBits = l32_BITS Else lNumBits = l16_BITS
ReDim dPlaceArray(0 To lNumBits)
ReDim lReturn(0 To lNumBits)
For lCount = LBound(dPlaceArray) To UBound(dPlaceArray)
dPlaceArray(lCount) = 2 ^ lCount
Next lCount
For lCount = UBound(dPlaceArray) To LBound(dPlaceArray) Step -1
If CBool(dPlaceArray(lCount) And lDecimal) Then
If bReturnArray Then
lReturn(lCount) = 1
Else
szReturn = szReturn & Chr$(49)
End If
Else
If bReturnArray Then
lReturn(lCount) = 0
Else
szReturn = szReturn & Chr$(48)
End If
End If
Next lCount
If bReturnArray Then
vDecimalToBinary = lReturn()
Else
vDecimalToBinary = szReturn
End If
End Function