Results 1 to 8 of 8

Thread: Help: Convert entire string to its ascii values?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    4

    Help: Convert entire string to its ascii values?

    Hi i'm a completly new at VB programing have done some C# but no VB, what i was wondering is there a really simple way to convert a string of text that a user enters into a text box convert it into its ascii value and display it in another textbox?

    I was hoping something like this would work

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    TextBox2.Text = Asc(TextBox1.Text)

    But it only puts the first ascii value in TextBox2 and then can't do anymore.

    Thanks for reading, also sorry if this is in the wrong section.

    Cheers MonsterRock

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help: Convert entire string to its ascii values?

    The Asc function will convert a Char to its equivalent ASCII value. If you pass a String then that gets implicitly converted to a Char, which means dropping all but the first character. If you want to convert the whole string then you need to call Asc on each character in the string, which means using a loop, e.g.
    vb.net Code:
    1. For Each ch As Char In myString
    2.     MessageBox.Show(ch & " = " & Asc(ch))
    3. Next ch
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    4

    Re: Help: Convert entire string to its ascii values?

    Thanks heaps! Thats works but not exactly how i wanted it to.

    Say if type A into the top text box the bottom one says A=65

    And then if i press B after A it erases the second text box and says B=66

    Is there a way to keep 65 aswell as 66?

    Thanks again for your help!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help: Convert entire string to its ascii values?

    If I get a dog and say his name is Spot then his name is Spot. If I then say his name is Rover his name is Rover, not SpotRover. If I wanted his name to be SpotRover then I'd have to add the new name to his old name, not replace his old name with the new one.

    Now let;s say that the dog was actually a TextBox and the name was actually the Text property. It follows that you have to add the new text to the existing text, not replace it. The existing text is a string. The new text is a string. How do you add two strings together?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    4

    Re: Help: Convert entire string to its ascii values?

    How do you add two strings together?

    Sorry i don't know.
    Last edited by MonsterRock; Aug 28th, 2007 at 09:59 PM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help: Convert entire string to its ascii values?

    I could be wrong but I'm guessing you have already concatenated two strings at least once, e.g.
    vb.net Code:
    1. Dim str As String = "string1" & "string2"
    In your case the existing string and the final string are both the Text property of your TextBox, so that would become:
    vb.net Code:
    1. myTextBox.Text = myTextBox.Text & "string2"
    or in the short form:
    vb.net Code:
    1. myTextBox.Text &= "string2"
    A preferable way with a TextBox though is like this:
    vb.net Code:
    1. myTextBox.AppendText("string2")
    Last edited by jmcilhinney; Aug 28th, 2007 at 09:59 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    4

    Re: Help: Convert entire string to its ascii values?

    Right got it thanks! Sorry bit slow back there.

  8. #8
    Member
    Join Date
    Mar 2005
    Posts
    47

    Re: Help: Convert entire string to its ascii values?

    Thanks, good help for me too

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