|
-
May 22nd, 2003, 09:57 AM
#1
Thread Starter
PowerPoster
Convert a string to binary, and back.
This should be a simple one,
I need to convert a string into a binary string. Then I need to take that binary string, and convert it back to a regular ascii string.
Anyone know how?
-
May 22nd, 2003, 11:47 AM
#2
Frenzied Member
VB Code:
'string to binary
'second argument converts the string from a base 10 number
'other valid arguments are 2, 8 & 16
'Method converts to a 32 bit Signed integer.
Dim i as Int
i = System.Convert.ToInt32 ("12345", 10)
'binary to string
Dim s as String
s = i.ToString()
Other valid conversions
ToInt16 - converts to a 16 bit signed integer
ToInt64 - converts to a 64 bit signed integer
ToUInt16 - converts to a 16 bit unsigned integer
ToUInt32 - converts to a 32 bit unsigned integer
ToUInt64 - converts to a 64 bit unsigned integer
Last edited by Memnoch1207; May 22nd, 2003 at 11:57 AM.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
May 22nd, 2003, 12:04 PM
#3
Thread Starter
PowerPoster
Thanks, but I don't think that will work.
I have a string, lets say:
"Hello, how are you."
I want to convert that to binary equivalent.
-
May 22nd, 2003, 12:22 PM
#4
Sleep mode
-
May 22nd, 2003, 12:33 PM
#5
Hyperactive Member
Originally posted by hellswraith
Thanks, but I don't think that will work.
I have a string, lets say:
"Hello, how are you."
I want to convert that to binary equivalent.
I don't quite understand. That is binary (ascii characters=bytes=8bits per byte).
Here's how you convert a string to a byte array (if that is indeed what you're asking):
Code:
Dim strHello As String = "Hello there, how are you?"
Dim btHello() As Byte
btHello = System.Text.ASCIIEncoding.ASCII.GetBytes(strHello)
This should also work with Unicode strings because a unicode character is nothing more than 2 asciii characters (if not, there are many more Encoding classes in the Text namespace that should do what you need, and you use them the same way).
I hope I didn't misunderstand your question.
Last edited by Hu Flung Dung; May 22nd, 2003 at 12:46 PM.
-
May 22nd, 2003, 12:45 PM
#6
Thread Starter
PowerPoster
Ok, this will make it easier to understand, I want to do something like this:
http://nickciske.com/tools/binary.php
-
May 22nd, 2003, 12:55 PM
#7
Hyperactive Member
I don't think there is a built in function for that. Maybe you could make a for-loop that counts in binary by either adding 1 to an integer or multiplying it by 10. Don't ask me for this algorithm though.
EDIT:
Just wanted to say that a recursive function might be easier to use for this than a for-loop, but I don't know
-
May 22nd, 2003, 02:49 PM
#8
-
May 23rd, 2003, 12:41 AM
#9
Thread Starter
PowerPoster
I got it working, but it wasn't as pretty as I would have liked. You can check it out here.
http://www.variantx.com/Main/DeveloperTools.aspx
-
May 23rd, 2003, 01:02 AM
#10
Frenzied Member
Hellswraith...I checked out your resume...and noticed some spelling errors (a pet peeve of mine)...just thought I would let you know...I don't think employers like to see spelling errors from educated individuals...especially when you can use a spell checker.
under RELEVANT EXPERIENCE
2nd sentence
*proceedures should be procedures
2nd paragraph 1st sentence
*files is printed twice
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
May 23rd, 2003, 05:10 AM
#11
Sleep mode
Hmm , it worked fine for me . Did you get any sort of errors ?
-
May 23rd, 2003, 06:23 AM
#12
Frenzied Member
I got it working, but it wasn't as pretty as I would have liked. You can check it out here.
I dont know how you accomplished that, but I use this:
VB Code:
Dim mytext() As Char = "This is a test"
Dim c As Char, s,bs As String, i as Integer
For Each c In mytext
s += Integer.Parse(Convert.ToString(Asc(c), 2)).ToString("00000000")
Next
MessageBox.Show("Converted to binary: " & s)
'Edits: Forgot to include binary to string: here is the code:
For i = 0 To s1.Length - 1 Step 8
bs += Chr((Convert.ToByte(s.Substring(i, 8), 2)))
Next
MessageBox.Show("Converted back to String: " & bs)
Last edited by Lunatic3; May 23rd, 2003 at 07:35 AM.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
May 23rd, 2003, 10:33 AM
#13
Thread Starter
PowerPoster
Originally posted by Memnoch1207
Hellswraith...I checked out your resume...and noticed some spelling errors (a pet peeve of mine)...just thought I would let you know...I don't think employers like to see spelling errors from educated individuals...especially when you can use a spell checker.
under RELEVANT EXPERIENCE
2nd sentence
*proceedures should be procedures
2nd paragraph 1st sentence
*files is printed twice
Thanks, you scared me, I thought I was sending those mistakes out on my 'real' resume when applying for jobs. I went straight for the word document and found there was no error in there. That was relief. Thanks for the heads up on the web based one, I have changed those.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|