Question involving text boxes...

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 18
Reputation: mark192 is an unknown quantity at this point 
Solved Threads: 0
mark192 mark192 is offline Offline
Newbie Poster

Question involving text boxes...

 
0
  #1
Feb 21st, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,996
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Question involving text boxes...

 
0
  #2
Feb 21st, 2008
To convert one char to ASCII value:

  1. Dim ch As Char
  2. ch = "A"
  3. MessageBox.Show(Asc(ch))
or:
  1. Dim ch As Char
  2. Dim intg as Integer
  3. ch = "A"
  4. intg = Asc(ch)
  5. MessageBox.Show(intg)

To convert a string to ASCII:

  1. Dim str As String
  2. Dim cnt As Integer = 0
  3. str = "abcd"
  4. While cnt < str.Length
  5. MessageBox.Show(Asc(str(cnt)))
  6. cnt += 1
  7. End While

Niek
Last edited by niek_e; Feb 21st, 2008 at 6:36 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 18
Reputation: mark192 is an unknown quantity at this point 
Solved Threads: 0
mark192 mark192 is offline Offline
Newbie Poster

Re: Question involving text boxes...

 
0
  #3
Feb 21st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,996
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Question involving text boxes...

 
0
  #4
Feb 21st, 2008
Sure, just add each ASCII value to a new 'total' string:
  1. Dim total As String = ""
  2. Dim str As String = "test"
  3.  
  4. Dim cnt As Integer = 0
  5.  
  6. While cnt < str.Length
  7. total += Asc(str(cnt)).ToString + "-"
  8. cnt += 1
  9. End While
  10.  
  11. 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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 18
Reputation: mark192 is an unknown quantity at this point 
Solved Threads: 0
mark192 mark192 is offline Offline
Newbie Poster

Re: Question involving text boxes...

 
0
  #5
Feb 21st, 2008
thanks a lot!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 18
Reputation: mark192 is an unknown quantity at this point 
Solved Threads: 0
mark192 mark192 is offline Offline
Newbie Poster

Re: Question involving text boxes...

 
0
  #6
Feb 21st, 2008
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...
Last edited by mark192; Feb 21st, 2008 at 8:04 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,996
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Question involving text boxes...

 
0
  #7
Feb 21st, 2008
That's alot more difficult. How does this string look? Do you still have the delimiter ( - ) in it? Please show an example

Niek
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 18
Reputation: mark192 is an unknown quantity at this point 
Solved Threads: 0
mark192 mark192 is offline Offline
Newbie Poster

Re: Question involving text boxes...

 
0
  #8
Feb 21st, 2008
  1. Dim total As String = ""
  2. Dim str As String = TextBox1.Text
  3. Dim cnt As Integer = 0
  4. Dim total3 As String = ""
  5. While cnt < str.Length
  6. total += Asc(str(cnt)).ToString + "-"
  7. cnt += 1
  8. End While
  9. Dim total1 As String = ""
  10. Dim str1 As String = TextBox2.Text
  11. Dim cnt1 As Integer = 0
  12. While cnt1 < str1.Length
  13. total1 += Asc(str1(cnt1)).ToString + "-"
  14. cnt1 += 1
  15. End While
  16. total3 = total + total1
  17. 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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,996
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Question involving text boxes...

 
0
  #9
Feb 21st, 2008
Originally Posted by mark192 View Post
[
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:
  1. While cnt < str.Length
  2. total += Asc(str(cnt)).ToString
  3. If cnt < str.Length() - 1 Then
  4. total += "-"
  5. End I
  6. cnt += 1
  7. 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

Niek
Last edited by niek_e; Feb 21st, 2008 at 8:52 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,996
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Question involving text boxes...

 
0
  #10
Feb 21st, 2008
On second thought, this shouldn't be too difficult:
  1. Dim total As String = "97-98-99-100"
  2.  
  3. Dim buffer As String
  4. Dim ascii As String()
  5. Dim output As String = ""
  6. ascii = total.Split("-")
  7. For Each buffer In ascii
  8. output += Chr(Convert.ToInt32(buffer))
  9. Next
  10.  
  11. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum


Views: 1180 | Replies: 10
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC