Public Function basBinStr(ValIn As Long, _
Optional MaxLen As Integer = 10) _
As String
'Michael Red, 11/26/2001
'Function to return the Binary representation of
'A Numeric (Long) The optional "MaxLen" is just the
'number of "Characters" of the string to Return (from
'the RIGHT!) Used just to generate all the possible
'combinations of "1" and "0" within a fixed filed size
'(up to 32)
Dim CurVal As Long
Dim TempVal As Long
Dim Indx As Integer
Dim RtnStr As Variant
Dim TmpStr As String
Dim BitSet(30) As String * 1
Indx = 0
Do While Indx <= UBound(BitSet)
BitSet(Indx) = "0"
Indx = Indx + 1
Loop
RtnStr = "0"
CurVal = ValIn
Indx = UBound(BitSet)
Do Until Indx < 0
TempVal = CurVal - (2 ^ Indx)
If (TempVal >= 0) Then
'This Bit Needs to Be Set,
'The String is A "1" & Indx - 1 Zeroes
BitSet(Indx) = 1
CurVal = CurVal - 2 ^ Indx
End If
Indx = Indx - 1
Loop
Indx = UBound(BitSet)
Do Until Indx < 0
RtnStr = RtnStr & BitSet(Indx)
Indx = Indx - 1
Loop
basBinStr = right(RtnStr, MaxLen)
End Function
Public Function basAllBins()
'Function to Demonstrate basBinStr function
'Which is just to create "Bit" patterns
'Michael Red, 11/26/2001
Dim Idx As Long
Idx = 0
Do While Idx <= 2047
Debug.Print basBinStr(Idx, 11)
Idx = Idx + 1
Loop
End Function