changing all ASC integer -1..?

Joined
Jan 26, 2005
Messages
5
Programming Experience
Beginner
hi

i am learning VB.net with out any other knowledge of programming, it's going ok but i'm stuck on a basic text encoding problem where i have to change all ASC code by -1,,,, i like to try and work everything out my self and bimble my way,, but i just can work this out at the mo,, i have worked out how to encode 1 letter,
VB.NET:
[color=#0000ff]Private[/color][color=#0000ff]Sub[/color] Enc_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] btnEnc.Click
 
[color=#0000ff]Dim[/color] MyInt [color=#0000ff]As[/color][color=#0000ff]Integer 
[/color][color=#0000ff]Dim[/color] MyChar [color=#0000ff]As[/color][color=#0000ff]Char
 
[/color]MyInt = Asc(txt1.Text) - 1 
MyChar = Chr(MyInt) 
txt1.Text = Format(MyChar, "")
 
[color=blue]E[/color][color=#0000ff]nd[/color][color=#0000ff]Sub[/color]


> change every thing in a text box to upper or lower case with out having to click a button
VB.NET:
[color=#0000ff][color=#000000][/color][/color]
[color=#0000ff][color=#0000ff]Private[/color][color=#0000ff]Sub[/color][color=#000000] Form1_Load([/color][color=#0000ff]ByVal[/color][color=#000000] sender [/color][color=#0000ff]As[/color][color=#000000] System.Object, [/color][color=#0000ff]ByVal[/color][color=#000000] e [/color][color=#0000ff]As[/color][color=#000000] System.EventArgs) [/color][color=#0000ff]Handles[/color][color=#0000ff]MyBase[/color][color=#000000].Load[/color]
 
txt1.CharacterCasing = CharacterCasing.Upper
 
[color=#0000ff]End[/color][color=#0000ff]Sub[/color]

> and with having to click a button
VB.NET:
[/color]
[color=#000000][color=#0000ff]PrivateSub[/color] Enc_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] btnEnc.Click[/color]
[color=#0000ff][color=#000000][/color][/color]
[color=#0000ff][color=#000000][color=#0000ff]Dim[/color] MyStr1 [color=#0000ff]As[/color][color=#0000ff]String
 
[/color]MyStr1 = UCase(rtb1.Text)
rtb2.Text = Format(MyStr1, "")
 
[color=blue]End[/color][color=#0000ff]Sub[/color]
> can also pick out a letter and encode it
VB.NET:
[color=#0000ff]PrivateSub[/color] Enc_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] btnEnc.Click
[color=#0000ff]Dim[/color] StrLength [color=#0000ff]As[/color][color=#0000ff]Integer
[/color][color=#0000ff]Dim[/color] MyStr [color=#0000ff]As[/color][color=#0000ff]String
[/color][color=#0000ff]Dim[/color] MyInt [color=#0000ff]As[/color][color=#0000ff]Integer
 
[/color]StrLength = Len(rtb1.Text) - 3
MyStr = Mid(rtb1.Text, Len(rtb1.Text) - StrLength + 1, 1)
MyInt = Asc(MyStr) - 1
rtb2.Text = Format(MyInt, "")
 
[color=blue]End[/color][color=#0000ff]Sub[/color]


but i just can't work out how to change all the Asc(##) -1, i know it's going to be simple, but i just can't seem to find my way to it....


thanks for any help


:)

[/color][/color]
 
Last edited:
here's how to do the Asc(x) - 1:

VB.NET:
dim CharHandler as System.Windows.Forms.KeyPressEventArgs
[size=2]MyInt = Asc(txt1.Text) - 1 
MyChar = Chr(MyInt) 
if CharHandler.KeyChar.IsLetter(MyChar) = True then
[/size][size=2]txt1.Text = CharHandler.KeyChar.ToUpper(MyChar)
else
[/size][size=2]txt1.Text = MyChar[/size]
[size=2]End If
[/size]
 
hi

thanks for getting back, that bit of code just changes the first letter back -1 asc() and then turns it upper case though....
i need to change the whole word(s) letters all back -1 so "hello" would become "gdkkn", and "Hello World" > "Gdkkn Vnqkc"

any ideas...



thanks again for you help..


:)
 
VB.NET:
Friend Function Encrypt(ByVal Entry As String) As String
		Dim sout As String = ""
		Dim p As Integer = 0
		Dim sc As String
		Dim nc As Integer
		For p = 1 To Len(Entry) Step 1
			sc = Mid(Entry, p, 1)
			nc = Asc(sc) - 1
			sout = sout + Chr(nc)
		Next p
		Return sout
	End Function

	Friend Function Decrypt(ByVal Entry As String) As String
		Dim sout As String = ""
		Dim p As Integer = 0
		Dim sc As String
		Dim nc As Integer
		For p = 1 To Len(Entry) Step 1
			sc = Mid(Entry, p, 1)
			nc = Asc(sc) + 1
			sout = sout + Chr(nc)
		Next p
		Return sout
	End Function

the Encrypt one does Asc -1 and the Decrypt one does Asc +1, alsothese do change space characters as well to have it skip spaces(aka leave spaces alone) just add a check for em

VB.NET:
For p = 1 To Len(Entry) Step 1
			sc = Mid(Entry, p, 1)
if sc <> " " then
			nc = Asc(sc) + 1
			sout = sout + Chr(nc)
end if
		Next p
 
Last edited:
hi

thanks,

i was working in that direction at one point, making a loop that moved on to the next letter using the loop counter but it would just do the last letter, because i was missing the

sout = sout + Chr(nc)

bit so it would over write the letter each time, and then the baby started crying and i lost my thought thread and ended taking out the loop cos it wasn't working....

anyway, i don't know how to use the Friend Function Encrypt() in/with a button, so i changed it a bit

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] btnGo_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] btnGo.Click
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] MyStr [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]String
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] MyInt [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sout [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = ""
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] C [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = 0
 
[/size][size=2][color=#0000ff]For[/color][/size][size=2] C = 1 [/size][size=2][color=#0000ff]To[/color][/size][size=2] Len(rtb1.Text) [/size][size=2][color=#0000ff]Step[/color][/size][size=2] 1[/size]
 
[size=2]
MyStr = Mid(rtb1.Text, C, 1)
MyInt = Asc(MyStr) - 1
sout = sout + Chr(MyInt)
 
[/size][size=2][color=#0000ff]Next[/color][/size][size=2] C [/size]
 
[size=2]
rtb2.Text = Format(Sout, "")
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub[/color][/size]


it seemed to do the trick, although this code only works if there are two text boxes, and i have to make an app that has one text box and two buttons, i think it'll need to make a copy of the string and use that to encode from so it's not looking at the txtBox for the next letter..... but i'm a bigger step closer to it now,

thanks for your help


adam


just thought i'd say it's done

VB.NET:
 [size=2]
[/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Enc_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] btnEnc.Click
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] MyStr [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] MyInt [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] Str [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] Sout [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = ""[/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] C [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = 0[/size]
[size=2]Str = [/size][size=2][color=#0000ff]String[/color][/size][size=2].Copy(txt1.Text)
[/size][size=2][color=#0000ff]For[/color][/size][size=2] C = 1 [/size][size=2][color=#0000ff]To[/color][/size][size=2] Len(txt1.Text) [/size][size=2][color=#0000ff]Step[/color][/size][size=2] 1
 
MyStr = Mid(Str, C, 1)
MyInt = Asc(MyStr) - 1
Sout = Sout + Chr(MyInt)
[/size][size=2][color=#0000ff]Next[/color][/size][size=2] C
 
txt1.Text = Format(sout, "")
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size]


thanks again for your help....
 
Last edited:
VB.NET:
Private[size=2][color=#0000ff] [/color][/size]Sub[size=2] btnEnc_Click([/size]ByVal [size=2]sender [/size]As[size=2] System.Object, [/size]ByVal[size=2] e [/size]As[size=2] System.EventArgs) [/size]Handles[size=2] btnEnc.Click
txt1.text = [/size]Encrypt(txt1.text)[size=2]
End Sub
[/size]

what this does is when you click the button it calls the encryptfunction passing it the text in txt1 then when the functionis done the text in txt1 is replaced with the returned value
 
hi'ya

ah yes i see, and that way you could use the bit of Encrypt code anywhere else and with out having to type it out again and again.....

thats a far neater way of arranging the app, i'll have to start thinking that way...


thanks again for your help




adam,
 
not a problem, if there's any two places in code that uses the sameroutine put it in a sub and call that sub from the multipleplaces, it makes for much easier debugging and if you need to changethat sub, you've only got 1 place to do it and not 2, 3, 4, etc...

subs and functions are very handly use them often i usuallyadd a code module to every project and dim all subs/functions as"Friend" so it can be accessed throught the app (spans multiple forms)
 
Back
Top