This demonstrates how to convert an ASCII integer (ie an integer returned by Chr(#)
) to its binary string.
Public Function cBIN(ByVal iC As Integer) As String
Dim X As Long, Y As Long, bC As Byte
cBIN = ""
X = 256
For Y = 1 To 8
bC = 0
X = X / 2
If iC >= X Then
bC = 1
iC = iC - X
End If
cBIN = cBIN & bC
Next Y
End Function
Call this function by doing a simple...
strBinary = cBIN(MyInteger)
Alternatively, you can use the following.
Private Function DecToBin(ByVal dIn As Double) As String
DecToBin = ""
While dIn >= 1
DecToBin = IIf(dIn Mod 2 = 0, "0", "1") & DecToBin
dIn = dIn \ 2
Wend
End Function
Private Function BinToDec(ByVal sIn As String) As Double
Dim x As Integer
BinToDec = 0
For x = 1 To Len(sIn)
BinToDec = BinToDec + (CInt(Mid(sIn, x, 1)) * (2 ^ (Len(sIn) - x)))
Next x
End Function
Comments