
November 7th, 2006, 06:21 AM
|
 |
Moderator From Beyond
|
|
Join Date: Sep 2004
Location: Israel
|
|
VBScript/classic ASP - String To Ascii and Ascii To String
Use the code below to convert whole string into Ascii,
and vice versa - for example the below "hello world"
string would be converted to:
"104101108108111032119111114108100"
and passing this to AsciiToString function would give
"hello world" again.
Code:
<%
Function StringToAscii(str)
Dim result, x
StringToAscii = ""
If Len(str)=0 Then Exit Function
If Len(str)=1 Then
result = Asc(Mid(str, 1, 1))
StringToAscii = Left("000", 3-Len(CStr(result))) & CStr(result)
Exit Function
End If
result = ""
For x=1 To Len(str)
result = result & StringToAscii(Mid(str, x, 1))
Next
StringToAscii = result
End Function
Function AsciiToString(str)
Dim result, x
AsciiToString = ""
If Len(str)<3 Then Exit Function
If Len(str)=3 Then
AsciiToString = Chr(CInt(str))
Exit Function
End If
result = ""
For x=1 To Len(str) Step 3
result = result & AsciiToString(Mid(str, x, 3))
Next
AsciiToString = result
End Function
'usage
Dim myString, strASCII
myString = "hello world"
strASCII = StringToAscii(myString)
Response.Write("original string: " & myString & "<br />")
Response.Write("ASCII: " & strASCII & "<br />")
Response.Write("back to string: " & AsciiToString(strASCII) & "<br />")
%>
|