Is there a way to assign the ASCII values from a text box to a label on a form? Say the text box had the character "a" inputed, how would i translate that into its ASCII value? Also is it possible to do this with a string of characters? Say I typed "aaa" and wanted it to return the value of these 3 letters in a row, say they are valued at 2 digits. "xxXXxx" Where the first xx is the first a the XX is the second a and the 2nd xx is the last a? I know i could do this through a large serious of ifs but there has to be an easier way...

To convert one char to ASCII value:

Dim ch As Char
        ch = "A"
        MessageBox.Show(Asc(ch))

or:

Dim ch As Char
        Dim intg as Integer
        ch = "A"
        intg = Asc(ch)
        MessageBox.Show(intg)

To convert a string to ASCII:

Dim str As String
        Dim cnt As Integer = 0
        str = "abcd"
        While cnt < str.Length
            MessageBox.Show(Asc(str(cnt)))
            cnt += 1
        End While

thanks, this is exactly what i was looking for, however the string line of code seems to only return the last character's value in the string. Is there a way to return every characters value in the string on one label? Say "dd" = 100100 rather than "dd" = 100...

Thanks

Sure, just add each ASCII value to a new 'total' string:

Dim total As String = ""
        Dim str As String = "test"

        Dim cnt As Integer = 0

        While cnt < str.Length
            total += Asc(str(cnt)).ToString + "-"
            cnt += 1
        End While

        MessageBox.Show(str + " in ASCII =" + total)

I've added a delimiter (-) to the string, to make it more readable.
Now just put something like : Label1.Text() = total in your code, where Label1 is your textcontrol and your done!

thanks a lot!

Sorry to be a bother, but I was wondering how I could convert back.

I've figured out as much as chr is the function i want to use (i think)

and i figured i would just try a similar line of code but replace asc with chr, but no luck... any help on this? again thanks

Edit: I did get it to work with an integer but cant seem to do it with a string...

That's alot more difficult. How does this string look? Do you still have the delimiter ( - ) in it? Please show an example

Dim total As String = ""
        Dim str As String = TextBox1.Text
        Dim cnt As Integer = 0
        Dim total3 As String = ""
        While cnt < str.Length
            total += Asc(str(cnt)).ToString + "-"
            cnt += 1
        End While
        Dim total1 As String = ""
        Dim str1 As String = TextBox2.Text
        Dim cnt1 As Integer = 0
        While cnt1 < str1.Length
            total1 += Asc(str1(cnt1)).ToString + "-"
            cnt1 += 1
        End While
        total3 = total + total1
        Label3.Text = total3

so i combined 2 text boxes ascii values together into one label, and now i want to know if you can get it back, also it doesn't matter so much but is there a way to remove the stray "-" at the end of the last number?

[
also it doesn't matter so much but is there a way to remove the stray "-" at the end of the last number?

Sure, just change the while loop to:

While cnt < str.Length
            total += Asc(str(cnt)).ToString
            If cnt < str.Length() - 1 Then
                total += "-"
            End I
                cnt += 1
        End While

I've added an 'if' that checks if the last char is reached. If not, then add a "-"

For the conversion back to chars: you should use something like total.split() to split the total string back to an array of strings each containing 1 ASCIInumber. Then use chr() to convert it back to Chars.

One more small thing: you don't have declare that much variables, just re-use the ones you've already declared. This saves memory

On second thought, this shouldn't be too difficult:

Dim total As String = "97-98-99-100"

Dim buffer As String
Dim ascii As String()
Dim output As String = ""
ascii = total.Split("-")
For Each buffer In ascii
    output += Chr(Convert.ToInt32(buffer))
Next

MessageBox.Show(total + " in ASCII = " + output)

Please let me know if it works, I haven't tried it.

[edit]Now it works 100%

thanks, this is exactly what i was looking for, however the string line of code seems to only return the last character's value in the string. Is there a way to return every characters value in the string on one label? Say "dd" = 100100 rather than "dd" = 100...

Thanks

You could move your code to changed text event and each time you enter a character the listbox would display the ASCII code equivilant?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.