Gustav Brock
Gustav at cactus.dk
Wed Mar 21 12:59:56 CDT 2007
Hi Arthur
Don't know what forest you now wish to explore, but this could get you started:
Public Function DecToBin(ByVal lngNumber As Long, Optional bytLength As Byte) As String
' Returns string that represents the binary expression for lngNumber.
'
' If bytLength is specified, returned string will be filled with
' leading zeroes up to this length.
Dim strBin As String
While lngNumber > 0
strBin = (lngNumber Mod 2) & strBin
lngNumber = lngNumber \ 2
Wend
If bytLength > 0 Then
strBin = Right(String(bytLength, "0") & strBin, bytLength)
End If
DecToBin = strBin
End Function
- and the counterpart:
Public Function BinToDec(ByVal strBinary As String) As Long
' Returns decimal value of binary value expressed by strBinary.
' Returns -1 if strBinary expresses a value larger than a Long.
'
' Ignores characters in strBinary other than 0 and 1 to
' allow for input strings like "1001 0011 1000".
' Maximum power of two for a Long.
Const cintPowerMax As Integer = 31
Dim intPos As Integer
Dim intTwo As Integer
Dim lngNum As Long
Dim strChr As String * 1
Dim bytChr As Byte
For intPos = Len(strBinary) To 1 Step -1
' Read strBinary backwards.
strChr = Mid(strBinary, intPos - 0, 1)
If InStr("01", strChr) = 0 Then
' Character at position intPos is neither 0 nor 1.
' Ignore character.
Else
' Convert character to numeric value.
bytChr = Val(strChr)
If bytChr = 0 Then
' Don't add a value of zero.
Else
If intTwo >= cintPowerMax Then
' Overrun. Maximum value of a Long has been reached.
intPos = 0
lngNum = -1
Else
' Add binary value of character at position intPos.
lngNum = lngNum + (bytChr * 2 ^ intTwo)
End If
End If
' Raise power for next possible addition by one.
intTwo = intTwo + 1
End If
Next intPos
BinToDec = lngNum
End Function
/gustav
>>> artful at rogers.com 21-03-2007 18:28 >>>
How do I walk through the bits comprising any given byte of interest? Suppose an input string such as "B". I want to walk through the bits comprising this byte and ideally create an array of those values. I am not sure even how to begin.
A.