Computing.Net > Forums > Programming > converting text to binary

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

converting text to binary

Reply to Message Icon

Name: Matt S
Date: December 19, 2002 at 09:32:48 Pacific
OS: Win 98
CPU/Ram: PII 400, 192Mb RAM
Comment:

Hey, does anyone know how to convert a message in a text box into binary? and vice versa? cheers.



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: December 19, 2002 at 09:39:41 Pacific
Reply:

Do you mean replace the character with its binary ascii code? (01000001 for 'A' etc.) What language?


0

Response Number 2
Name: Matt S
Date: December 19, 2002 at 09:55:32 Pacific
Reply:

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


0

Response Number 3
Name: SN
Date: December 19, 2002 at 10:00:20 Pacific
Reply:

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


0

Response Number 4
Name: Matt S
Date: December 19, 2002 at 10:26:20 Pacific
Reply:

aha, yes i thought there may be something I'd missed out :oP I'm usinc VB6.


0

Response Number 5
Name: SN
Date: December 19, 2002 at 11:10:58 Pacific
Reply:

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 0001

This 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


0

Related Posts

See More



Response Number 6
Name: Matt S
Date: December 19, 2002 at 15:07:23 Pacific
Reply:

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)


0

Response Number 7
Name: Matt S
Date: December 19, 2002 at 15:09:54 Pacific
Reply:

anyone with a 'more elegant solution' is welcome to add their input :o)


0

Response Number 8
Name: HiJinx
Date: December 19, 2002 at 15:59:04 Pacific
Reply:

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

End Function
------------------------
Private Function LetterToBinary(intAscii) As String

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

Next

End Function
------------------------
Private Function BinaryToString(strBin As String) As String

Dim x As Integer

For x = 1 To Len(strBin) Step 8
BinaryToString = BinaryToString & GetAsciiLetter(Mid(strBin, x, 8))
Next

End Function
------------------------
Private Function GetAsciiLetter(strBin As String) As String

Dim x As Integer
Dim intAsciiVal As Integer

For x = 0 To 7

If Mid(strBin, x + 1, 1) = "1" Then
intAsciiVal = intAsciiVal + 2 ^ (7 - x)
End If

Next

GetAsciiLetter = Chr(intAsciiVal)

End Function


0

Response Number 9
Name: Matt S
Date: December 22, 2002 at 05:06:34 Pacific
Reply:

right, this looks like it'll work, but where does all this code go?

thanks a lot


0

Response Number 10
Name: HiJinx
Date: December 22, 2002 at 08:56:34 Pacific
Reply:

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 Sub

Private Sub Command2_Click()
Text3.Text = BinaryToString(Text2.Text)
End Sub

Run it, type whatever you want in Text1, then click Command1, then Command2 to see the conversions work.


0

Response Number 11
Name: Matt S
Date: December 22, 2002 at 12:36:15 Pacific
Reply:

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


0

Response Number 12
Name: HiJinx
Date: December 22, 2002 at 13:22:31 Pacific
Reply:

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


0

Response Number 13
Name: Matt S
Date: December 22, 2002 at 14:14:59 Pacific
Reply:

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


0

Response Number 14
Name: THS
Date: January 28, 2003 at 17:37:08 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

text processing batch IP address message



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: converting text to binary

How to convert Ascii to binary www.computing.net/answers/programming/how-to-convert-ascii-to-binary/829.html

converting to binary,octal,hexa www.computing.net/answers/programming/converting-to-binaryoctalhexa/11823.html

decimal to binary conversion help!! www.computing.net/answers/programming/decimal-to-binary-conversion-help/4822.html