当前位置: 移动技术网 > IT编程>开发语言>Asp > ASP编码和解码函数详解

ASP编码和解码函数详解

2017年12月08日  | 移动技术网IT编程  | 我要评论

用asp开发的时候遇到一个解码问题。虽然在asp中使用request获取编码过url字符串会自动解码,但是request.binaryread(request.totalbytes)取得post数据时却不会解码,所以只能手动进行解码。
asp解码函数:

function urldecode(enstr) 
 dim destr,strspecial 
 dim c,i,v 
  destr=""
  strspecial="!""#$%&'()*+,.-_/:;<=>?@[/]^`{|}~%"
  for i=1 to len(enstr) 
   c=mid(enstr,i,1) 
   if c="%" then 
    v=eval("&h"+mid(enstr,i+1,2)) 
    if instr(strspecial,chr(v))>0 then 
     destr=destr&chr(v) 
     i=i+2 
    else
     v=eval("&h"+ mid(enstr,i+1,2) + mid(enstr,i+4,2)) 
     destr=destr & chr(v) 
     i=i+5 
    end if
   else
    if c="+" then 
     destr=destr&" "
    else
     destr=destr&c 
    end if
   end if
  next 
  urldecode=destr 
end function

只是个人爱好,自己研究了一下编码的实现思路,最后自己写了一个编码函数,提供大家参考。注:asp有内置的编码函数,即是server.urlencode。

asp编码函数:

private function urlencoding(vstrin) 
  strreturn = ""
  for i = 1 to len(vstrin) 
  thischr = mid(vstrin,i,1) 
  if abs(asc(thischr)) < &hff then 
  strreturn = strreturn & thischr 
  else 
  innercode = asc(thischr) 
  if innercode < 0 then 
  innercode = innercode + &h10000 
  end if 
  hight8 = (innercode and &hff00)/ &hff 
  low8 = innercode and &hff 
  strreturn = strreturn & "%" & hex(hight8) & "%" & hex(low8) 
  end if 
  next 
  urlencoding = strreturn 
end function

建议大家在中文编码的时候,还是使用asp 内置的函数。虽然上面这个编码函数测试过n 遍了,没有发现问题,但是以防万一存在bug。

以上就是关于asp编码和解码函数,希望对大家的学习有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网