Monday, September 29, 2008

Buy Neon Slouch Socks

function to convert between ASCII and Characters on ASP

This function can serve for example when we want to make a user validation respetanto case sensitive (which makes no ASP). We have the option to convert the characters by their ASCII value and vice versa, the function is:

StringToAscii Function (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

End Function 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) \u0026lt;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 & "
)
Response.Write (" ASCII: "& strASCII &"
)
Response.Write ("back to string:" & AsciiToString (strASCII) & "
)



0 comments:

Post a Comment