| | |
Question involving text boxes...
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 18
Reputation:
Solved Threads: 0
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:
or:
To convert a string to ASCII:
Niek
vb Syntax (Toggle Plain Text)
Dim ch As Char ch = "A" MessageBox.Show(Asc(ch))
vb Syntax (Toggle Plain Text)
Dim ch As Char Dim intg as Integer ch = "A" intg = Asc(ch) MessageBox.Show(intg)
To convert a string to ASCII:
vb Syntax (Toggle Plain Text)
Dim str As String Dim cnt As Integer = 0 str = "abcd" While cnt < str.Length MessageBox.Show(Asc(str(cnt))) cnt += 1 End While
Niek
Last edited by niek_e; Feb 21st, 2008 at 6:36 am.
Sure, just add each ASCII value to a new 'total' string:
I've added a delimiter (-) to the string, to make it more readable.
Now just put something like :
Niek
vb Syntax (Toggle Plain Text)
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!Niek
•
•
Join Date: Jan 2008
Posts: 18
Reputation:
Solved Threads: 0
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...
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...
Last edited by mark192; Feb 21st, 2008 at 8:04 am.
•
•
Join Date: Jan 2008
Posts: 18
Reputation:
Solved Threads: 0
VB.NET Syntax (Toggle Plain Text)
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?
vb Syntax (Toggle Plain Text)
While cnt < str.Length total += Asc(str(cnt)).ToString If cnt < str.Length() - 1 Then total += "-" End I cnt += 1 End While
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
Niek
Last edited by niek_e; Feb 21st, 2008 at 8:52 am.
On second thought, this shouldn't be too difficult:
Please let me know if it works, I haven't tried it.
Niek
[edit]Now it works 100%
vb Syntax (Toggle Plain Text)
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.
Niek
[edit]Now it works 100%
Last edited by niek_e; Feb 21st, 2008 at 8:55 am.
![]() |
Other Threads in the VB.NET Forum
- Previous Thread: SelectedIndexChanged in DropDownList won't trigger
- Next Thread: Nested tokens
Views: 1180 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2008 access advanced application array basic beginner browser button buttons center class click client code combo convert cuesent data database datagrid datagridview date datetimepicker design designer dissertation dissertations dissertationtopic eclipse excel exists fade filter forms function html images lib listview map mobile module msaccess net number objects open panel pdf picturebox picturebox2 port position print printing problem read refresh regex richtextbox right-to-left save search serial settings shutdown socket sorting sqldatbase sqlserver studio temperature textbox timer timespan transparency txttoxmlconverter usercontol validation vb vb.net vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web webbrowser winforms winsock wpf wrapingcode xml year






