ASP中实现的类似URLEncode的编码函数及对应解码函数

本文介绍了一种在ASP环境中实现的URL编码和解码函数,通过这些函数可以将特殊字符转换为适用于URL的形式,并能将其还原。编码函数会检查每个字符,将特定字符替换为百分号加上其十六进制ASCII值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ASP中实现的类似URLEncode的编码函数及对应解码函数 

<%
'Coding.inc.asp

Function Encode(Str)
 Dim Count, Pos, Ch, Code
 Dim SweetCh
 
 'SweetCh中表示不需要进行编码的字符
 SweetCh = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz_{}[]()"
 Encode = ""
 
 Count = Len(Str)
 Pos = 1
 Do While Pos<=Count
  Ch = Mid(Str, Pos, 1)
  
  Code = Asc(Ch)
  If Code>=0 And Code<256 Then  '汉字不予处理
   If Ch<>"%" Then
    If InStr(SweetCh, Ch)=0 Then
     Ch = "%" & Right("0" & Hex(Code), 2)
    End If
   Else
    Ch = "%25"
   End If
  End If
  
  Encode = Encode & Ch
  Pos = Pos + 1
 Loop
End Function

Function Decode(Str)
 Dim Count, Pos, Ch, Code
 
 Decode = ""
 
 Count = Len(Str)
 Pos = 1
 Do While Pos<=Count
  Ch = Mid(Str, Pos, 1)
  If Ch="%" Then
   If Pos+2<=Count Then
    Ch = Chr((InStr("0123456789ABCDEF", UCase(Mid(Str, Pos+1, 1)))-1) * 16 + InStr("0123456789ABCDEF",UCase(Mid(Str, Pos+2, 1))) - 1)
   Else
    '编码串不正确
    Ch = ""
   End If
   Pos = Pos + 2
  End If
  Decode = Decode & Ch
  Pos = Pos + 1
 Loop
End Function
%>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值