Library code snippets

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

  1. 28 Nov 2006 at 20:20

    Simple...

    Just run the code but save the character values to corresponding array elements instead.

    /the code

    Dim i as long, MyArray() As Integer

    Redim MyArray(Len(TheString)-1) As Integer

    For i = 1 to len(TheString)

       MyArray(i-1) = Asc(Mid(TheString,i,1))

    Next i

    /end code

    Now it's an array of integers.

  2. 18 May 2005 at 18:04

    but the inverse process:
    from a string to obtain a Array of integers(ASCII Codes for each caracter)?

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of ASCII to Binary string.

Leave a comment

Sign in or Join us (it's free).

Mike J

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!