Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 41,143 times

Related Categories

ASCII to Binary string

mrjdesign

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


©2001 - All Rights Reserved - MRJ Design
The source code samples and information pertained within this document are considered copyrighted material and may not be re-distributed by electronic or other media in any form or fashion whatsoever, withou

Comments

  • Re: [3570] ASCII to Binary string

    Posted by mrjdesign on 28 Nov 2006

    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(Th...

  • inverse question

    Posted by vserhio on 18 May 2005

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