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
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:
For Each ch As Char In myString
MessageBox.Show(ch & " = " & Asc(ch))
Next ch
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!
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?
Re: Help: Convert entire string to its ascii values?
How do you add two strings together?
Sorry i don't know.
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:
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:
myTextBox.Text = myTextBox.Text & "string2"
or in the short form:
vb.net Code:
myTextBox.Text &= "string2"
A preferable way with a TextBox though is like this:
vb.net Code:
myTextBox.AppendText("string2")
Re: Help: Convert entire string to its ascii values?
Right got it thanks! Sorry bit slow back there.
Re: Help: Convert entire string to its ascii values?
Thanks, good help for me too