分类: 系统运维
2010-12-06 13:47:25
'***********************************************************
'功能:实现在ASP中对字符的Escape编码和解码
'编码:xiaohongcheng
'日期:06/12/2010
'***********************************************************
'+-----------------------------------------------------------------
'Escape编码,实现JavaScript的escape编码方法。
'将小于255的标点等字符变为“%+其16进制ASCII码(两位数字,不足前面补0)”的形式,
'将大于255的字符变为“%u+其16进制Unicode码(四位数字,不足前面补0)”的形式。
'例如:我=>%u6211。
' 编码以后可以直接用JavaScript的unescape方法解码。
'------------------------------------------------------------------
Function EscapeCode(p_Message)
Dim m_char,m_asc,m_hex '字符,ASC码,16进制ASCII码
Dim m_temp '临时字符
Dim a_arc() 'ASC码数组
Dim i
ReDim a_arc(Len(p_Message))
For i = 0 To Len(p_Message) -1
m_char = Mid(p_Message,i+1,1)
m_asc = AscW(m_char)
If m_asc < 255 Then
If (m_char = "*") or(m_char = "+") _
or(m_char >= "-" And m_char <= "9" ) _
or(m_char >= "@" And m_char <= "Z" ) _
or(m_char = "_") _
or(m_char >= "a" And m_char <= "z" ) Then
a_arc(i) = m_char
Else
m_temp = Hex(m_asc)
If Len(m_temp) = 1 Then
a_arc(i) = "%0" & m_temp
ElseIf Len(m_temp) = 2 Then
a_arc(i) = "%" & m_temp
Else
a_arc(i) = "%u" & m_temp
End If
End If
Else
m_temp = Hex(m_asc)
If Len(m_temp) = 1 Then
a_arc(i) = "%u000" & m_temp
ElseIf Len(m_temp) = 2 Then
a_arc(i) = "%u00" & m_temp
ElseIf Len(m_temp) = 3 Then
a_arc(i) = "%u0" & m_temp
Else
a_arc(i) = "%u" & m_temp
End If
End If
Next
EscapeCode = Join(a_arc,"")
End Function
'对Escape编码后的字符串进行解码
Function EscapeDecode(p_Message)
EscapeDecode = p_Message ' 函数待完成
End Function