Results 1 to 13 of 13

Thread: Convert a string to binary, and back.

  1. #1

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464

    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?

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    VB Code:
    1. 'string to binary
    2. 'second argument converts the string from a base 10 number
    3. 'other valid arguments are 2, 8 & 16
    4. 'Method converts to a 32 bit Signed integer.
    5. Dim i as Int
    6. i = System.Convert.ToInt32 ("12345", 10)  
    7.  
    8. 'binary to string
    9. Dim s as String
    10. 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

  3. #3

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

  5. #5
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    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.

  6. #6

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Ok, this will make it easier to understand, I want to do something like this:
    http://nickciske.com/tools/binary.php

  7. #7
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    Originally posted by hellswraith
    Ok, this will make it easier to understand, I want to do something like this:
    http://nickciske.com/tools/binary.php
    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

  8. #8
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    hmm it should be easy to find some code for this. I think what you have to do is to convert each character of the string to ascii code, then you should convert the ascii code to binary.
    To convert a number to binary you basically keep dividing the number by 2 and you save the remainder. I dunno how to explain it. IE:

    91 is your number
    91\2 = 45 and it's remainder is 1 (91 mod 2 =1)
    now you look at 45:
    45\2=22 and 45 mod 2 =1
    now do the same with 22:
    22\2=11 and 22 mod 2 =0

    11\2=5 and 11 mod 2 =1
    5\2=2 and 5 mod 2=1
    2\2=1 and 2 mod 2=0
    1\2 is less than 0, STOP


    now you write down all the red numbers in REVERSE: 1011011. That's 91 in binary for the last part, I wrote the "1\2" in red. I think you always consider the last number to be 1. Just remember that when you convert an ascii to binary, it has to be 8 digits. So if you get something like 1101 you have to put 4 zeros next to it to make it 8 digits:00001101.

    I hope I'm not making a mistake here this was my 7th grade memory
    Last edited by MrPolite; May 22nd, 2003 at 02:54 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  9. #9

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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

  10. #10
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    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

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by hellswraith
    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
    Hmm , it worked fine for me . Did you get any sort of errors ?

  12. #12
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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:
    1. Dim mytext() As Char = "This is a test"
    2. Dim c As Char, s,bs As String, i as Integer
    3. For Each c In mytext
    4.      s += Integer.Parse(Convert.ToString(Asc(c), 2)).ToString("00000000")
    5. Next
    6. MessageBox.Show("Converted to binary: " & s)
    7. 'Edits: Forgot to include binary to string: here is the code:
    8. For i = 0 To s1.Length - 1 Step 8
    9.       bs += Chr((Convert.ToByte(s.Substring(i, 8), 2)))
    10. Next
    11. 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

  13. #13

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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
  •  



Click Here to Expand Forum to Full Width