Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Do you mean replace the character with its binary ascii code? (01000001 for 'A' etc.) What language?

yeah like that, im not sure what you mean by what language, but yeah I want to convert ascii text to binary.

By what language, I mean are you writing a program to do this, if so, what language are you programming in (C, C++, Java, Perl, etc.)are you doing it by hand, or is it a web page? Where is the text box?
-SN

I have never used VB, and I know Java has some methods to make this much faster, but assuming you have to do it all by yourself, you would get the string in the text box, take the first character, and cast it to an int.
This will give you the ascii code in decimal. Convert this to binary by integer dividing it by two, and recording the remainder until the quotient is zero. This is better explained by example:
A=$41=65
65/2=32 r1
32/2=16 r0
16/2=8 r0
8/2=4 r0
4/2=2 r0
2/2=1 r0
1/2=0 r1
The remainders make up the bits of the number from least significant to most significant, so your final answer is:
100 0001This process translates to code loop like:
char x=first char in text box
int y=(int)x;
String answer="";while (y !=0){
nextbit=y%2;
concatenate (nextbit, answer);
y=y/2;
}I'm sure somebody who knows VB will have a more elegant solution. Good luck,
-SN

ok thanks for that. looks a bit complex for m limited experience with VB, but I'm sure it'll prove v useful, thanks a lot :o)

Played for bit and came up with the following... use "StringToBinary" to convert a string to a binary sequence and "BinaryToString" to convert the digits back again. There's no error checking, so be careful what you feed BinaryToString. The formatting will get messed up in the post, so if you get errors, check the line spacing.
Private Function StringToBinary(strAscii As String) As String
Dim x As Integer
For x = 1 To Len(strAscii)
StringToBinary = StringToBinary & LetterToBinary(Asc(Mid(strAscii, x, 1)))
NextEnd Function
------------------------
Private Function LetterToBinary(intAscii) As StringDim x As Integer
For x = 7 To 0 Step -1
If intAscii >= 2 ^ x Then
LetterToBinary = LetterToBinary & "1"
intAscii = intAscii Mod 2 ^ x
Else
LetterToBinary = LetterToBinary & "0"
End IfNext
End Function
------------------------
Private Function BinaryToString(strBin As String) As StringDim x As Integer
For x = 1 To Len(strBin) Step 8
BinaryToString = BinaryToString & GetAsciiLetter(Mid(strBin, x, 8))
NextEnd Function
------------------------
Private Function GetAsciiLetter(strBin As String) As StringDim x As Integer
Dim intAsciiVal As IntegerFor x = 0 To 7
If Mid(strBin, x + 1, 1) = "1" Then
intAsciiVal = intAsciiVal + 2 ^ (7 - x)
End IfNext
GetAsciiLetter = Chr(intAsciiVal)
End Function

Just as example to show how to use the functions, put three text boxes and two command buttons on a form (set the multiline property of the textboxes to True).
Put in the code from above, now also add:
Private Sub Command1_Click()
Text2.Text = StringToBinary(Text1.Text)
End SubPrivate Sub Command2_Click()
Text3.Text = BinaryToString(Text2.Text)
End SubRun it, type whatever you want in Text1, then click Command1, then Command2 to see the conversions work.

"Put in the code from above"
put it where? My first (and very uneducated guess) would be to put it in general declarations, is this right?

You can put it almost anywhere, but don't try to paste it inside of another sub of function. I usually put my own subs and functions below code for controls, but that's just a personal preference.
Remember that you may have to fix the code a bit because it got broken up a little when getting posted. (a few "As Strings" got split, and "StringToBinary = StringToBinary & LetterToBinary(Asc(Mid(strAscii, x, 1)))" and "BinaryToString = BinaryToString & GetAsciiLetter(Mid(strBin, x, 8))" also got broken (each of the statements should be on one line).

Thanks a lot, this works, I put the code in general declarations and it works fine. you've been a wonderful audience. cheers all

Can anyone tell me how to convert "Are" into 7 bit binary. The method above gave me 24 bit binary but i want only 7 bit binary.

![]() |
text processing batch
|
IP address message
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |