ASCII to Binary string

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
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

You might also like...


Mike J


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is anticipated with distaste, performed with reluctance, and bragged about forever.” - Dan Kaminsky