zz实现在ASP中对字符的Escape编码

时间:07/11/2006 14:08:00   作者:ChenReal    阅读:158

'+-----------------------------------------------------------------
'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

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=263877

 

评论
0/200