We help IT Professionals succeed at work.

Reversing this code (getting UN-ASCII of the string)

Camillia
Camillia asked
on
Medium Priority
411 Views
Last Modified: 2012-08-13
This code reverses the string and gives me the ASCII of the string. Now I have the ASCII code , how can I get reverse the code and get the actuall string back again?
Dim test As String = "123456789012345"
 
        Dim lLen As Integer, lCtr As Integer
        Dim sChar As String
        Dim sAns As String = ""
 
        lLen = Len(test)
        For lCtr = lLen To 1 Step -1
            sChar = Chr(192 + Asc(Mid(test, lCtr, 1)))
            sAns = sAns & sChar
        Next
 
        TextBox1.Text = sAns

Open in new window

Comment
Watch Question

Your code doesn't give you the ASCII of the string (which would be a series of numbers), but a new string which is the original string reversed and with 192 added to the code of each character. For number characters as above (ASCII 48-57) this results in an output with codes in the range 240-249. You should be aware that inputting a string containing letters to your code will cause problems (as adding 192 to eg 'A'=65 gives a number outside the normal 0-255 ASCII range).

To reverse the code:



Dim sCode as String
sCode = TextBox1.Text
 
     Dim lLen As Integer, lCtr As Integer
        Dim sChar As String
        Dim sAns As String = ""
 
        lLen = Len(sCode)
        For lCtr = lLen To 1 Step -1
            sChar = Chr(192 + Asc(Mid(sCode, lCtr, 1)))
            sAns = sAns & sChar
        Next
 
Decoded version now held in sAns.

Open in new window

CERTIFIED EXPERT

Author

Commented:
let me try
CERTIFIED EXPERT

Author

Commented:
No, I get this error : Procedure or call is not valid. I get it on sChar:

 Dim sCode As String
        sCode = sAns  ' this is where I encode it (in my code)
        lLen = Len(sCode)
        For lCtr = lLen To 1 Step -1
            sChar = Chr(192 + Asc(Mid(sCode, lCtr, 1)))  ' I get the error heree\
            sAns = sAns & sChar
        Next

Sorry - mistake in my code (too much cut'n'paste)

Line 10 should read:
sChar = Chr(Asc(Mid(sCode, lCtr, 1)) - 192)

Open in new window

CERTIFIED EXPERT

Author

Commented:
oh, let me try.
CERTIFIED EXPERT

Author

Commented:
thanks, that worked

Explore More ContentExplore courses, solutions, and other research materials related to this topic.