Converting string between Asc() and chr()

MiserableMad

Active member
Joined
Feb 2, 2007
Messages
25
Programming Experience
Beginner
First of all I am a newbie...so if you want you can call me a "newbie" or a "freshy", either one is ok...

this is what I have so far *this is a code test that I am working on to get this conversion to work...

VB.NET:
Dim c AsChar
Dim s, b AsString
txtConverted.Text = Nothing
ForEach c In txtConvert.Text
s &= Asc(c)
Next
txtConverted.Text = s
ForEach c In s
b &= Chr(c)
Next
TextBox1.Text = b

but then I realized that each asc() char is more that one integer long, so that did not work...

*I did the for each loop because It only converts one char
but it underlines the "c" (in red) and comes up with this error...

C:\Documents and Settings\Dane\My Documents\Visual Studio Projects\Reg Test\Form1.vb(105): 'Char' values cannot be converted to 'Integer'. Use 'Microsoft.VisualBasic.AscW' to interpret a character as a Unicode value or 'Microsoft.VisualBasic.Val' to interpret it as a digit.

So I tried the b &= Chr(convert.tostring(c))

than it gave me a whole bunch of numbers like this...

The asc() works fine, it is the chr() that does not
 
Last edited:
Most characters translate to 2 or 3 digits integers. That means asc("A") = integer value 65, which you make a "65" string, then you are converting back "6" to char and "5" to a char. That doesn't add up to the original "A". Think your algoritm over again..
 
lol thanks for that nifty advice...

I found that out shortly after I made the code. The chr() only converts back only the first character in the string. Im thinking that I have to do a (for each char in word...loop) and add that letter to an array. Then treat the open the letters (assigned to the array(a()) and and that to my textbox. There is probally an easier way of doing this...but I am at a lose.

and these are the two msg's I keep getting

C:\Documents and Settings\Dane\My Documents\Visual Studio Projects\Test\Form1.vb(103): 'Char' values cannot be converted to 'Integer'. Use 'Microsoft.VisualBasic.AscW' to interpret a character as a Unicode value or 'Microsoft.VisualBasic.Val' to interpret it as a digit.

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from string "?" to type 'Integer' is not valid.
 
Chr() function doesn't take first character in the string at all, that function only accepts an integer input parameter. If you have a string "65" you can convert it to a integer with the CInt conversion function. When you have the integer value you can give it as input to the Chr function and it will translate it into a character.
 
Thank you for being so patient with me, I am not the greatest at this. This is the code I just tried...

VB.NET:
Dim s(1000), g As String
Dim c As Char
Dim b As Integer = 0
Dim d As Integer
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
For Each c In txt1.Text
s(b) = Asc(c)
b += 1
Next
txt1.Text = ""
For d = 0 To b
txt1.Text &= s(d)
Next
b = 0
End Sub
Private Sub bt_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bt.Click
For d = 0 To b
g &= Chr(s(d))
Next
txt1.Text = ""
txt1.Text = g
End Sub

and it works, but only if I want to encyrpt/decrypt while under the same paragraph while still in the same program.:confused:
 
Last edited:
I got it to work...I hate myself I am so stupid...this is what I did

VB.NET:
Dim s(4999), g, f As String
Dim c As Char
Dim b As Integer = 0
Dim d As Integer
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnEncyrpt.Click
For Each c In txt1.Text
s(b) = Asc(c) + 2
b += 1
Next
txt1.Text = ""
For d = 0 To b
txt1.Text &= Chr(s(d))
Next
End Sub
Private Sub bt_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnDecyrpt.Click
For Each c In txt1.Text
g = Asc(c) - 2
f &= Chr(g)
Next
txt1.Text = ""
txt1.Text = f
End Sub

One last question, is there a way to clear all the data in an array?
Thanks for your help.:) :) :)
 
Last edited:
Back
Top