<%'--------------------------------------------------------------------------

'==============================================================

'功能:ASP Server对象内置编码函数

'描写叙述:没有相应的解码函数

'==============================================================

Function VB_URLEncode(enStr)

VB_URLEncode = Server.URLEncode(enStr)

End Function

'==============================================================

'功能:Server.URLEncode()的解码函数

'描写叙述:眼下该函数还未完好

' 当本页面为UTF-8编码时。源字符串中包括例如以下格式子字符串时:

' "编码aa測aa试"

' 函数无法对VB_URLEncode()之后的编码进行解码

' 当本页面为GB2312编码是。该函数工作正常。

'==============================================================

Function VB_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_r("&h"+Mid(enStr,i+1,2))

If inStr(strSpecial,chr(v))>0 Then

deStr=deStr&chr(v)

i=i+2

Else

v=eval_r("&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

VB_URLDecode=deStr

End Function

'===========================================

'功能:对中文字符进行编码。由GB2312转换为UTF-8

'描写叙述:与UTF8toGB()互逆

' 编码后的格式可用于页面之间的数据传递,但无法

' 正确显示在HTML页面,须要UTF8toGB()解码。

'===========================================

Function VB_GBtoUTF8(szInput)

Dim wch, uch, szRet

Dim x

Dim nAsc, nAsc2, nAsc3

'假设输入參数为空,则退出函数

If szInput = "" Then

VB_GBtoUTF8 = szInput

Exit Function

End If

'開始转换

For x = 1 To Len(szInput)

'利用mid函数分拆GB编码文字

wch = Mid(szInput, x, 1)

'利用ascW函数返回每个GB编码文字的Unicode字符代码

'注:asc函数返回的是ANSI 字符代码,注意差别

nAsc = AscW(wch)

If nAsc < 0 Then nAsc = nAsc + 65536



If (nAsc And &HFF80) = 0 Then

szRet = szRet & wch

Else

If (nAsc And &HF000) = 0 Then

uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)

szRet = szRet & uch

Else

'GB编码文字的Unicode字符代码在0800 - FFFF之间採用三字节模版

uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _

Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _

Hex(nAsc And &H3F Or &H80)

szRet = szRet & uch

End If

End If

Next

VB_GBtoUTF8 = szRet

End Function

'===========================================

'功能:对中文字符进行编码。由UTF-8转换为GB2312

'描写叙述:VB_GBtoUTF8()的解码函数

'===========================================

Function VB_UTF8toGB(UTFStr)

For Dig=1 To len(UTFStr)

'假设UTF8编码文字以%开头则进行转换

If mid(UTFStr,Dig,1)="%" Then

'UTF8编码文字大于8则转换为汉字

If len(UTFStr) >= Dig+8 Then

GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))

Dig=Dig+8

Else

GBStr=GBStr & mid(UTFStr,Dig,1)

End If

Else

GBStr=GBStr & mid(UTFStr,Dig,1)

End If

Next

VB_UTF8toGB=GBStr

End Function



'UTF8编码文字将转换为汉字

Function ConvChinese(x)

A=split(mid(x,2),"%")

i=0

j=0

For i=0 To ubound(A)

A(i)=c16to2(A(i))

Next

For i=0 To ubound(A)-1

DigS=instr(A(i),"0")

Unicode=""

For j=1 To DigS-1

If j=1 Then

A(i)=right(A(i),len(A(i))-DigS)

Unicode=Unicode & A(i)

Else

i=i+1

A(i)=right(A(i),len(A(i))-2)

Unicode=Unicode & A(i)

End If

Next

If len(c2to16(Unicode))=4 Then

ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))

Else

ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))

End If

Next

End Function

'二进制代码转换为十六进制代码

Function c2to16(x)

i=1

For i=1 To len(x) step 4

c2to16=c2to16 & hex(c2to10(mid(x,i,4)))

Next

End Function



'二进制代码转换为十进制代码

Function c2to10(x)

c2to10=0

If x="0" Then Exit Function

i=0

For i= 0 To len(x) -1

If mid(x,len(x)-i,1)="1" Then c2to10=c2to10+2^(i)

Next

End Function

'十六进制代码转换为二进制代码

Function c16to2(x)

i=0

For i=1 To len(trim(x))

tempstr= c10to2(cint(int("&h" & mid(x,i,1))))

Do While len(tempstr)<4

tempstr="0" & tempstr

Loop

c16to2=c16to2 & tempstr

Next

End Function

'十进制代码转换为二进制代码

Function c10to2(x)

mysign=sgn(x)

x=abs(x)

DigS=1

Do

If x<2^DigS Then

Exit Do

Else

DigS=DigS+1

End If

Loop

tempnum=x

i=0

For i=DigS To 1 step-1

If tempnum>=2^(i-1) Then

tempnum=tempnum-2^(i-1)

c10to2=c10to2 & "1"

Else

c10to2=c10to2 & "0"

End If

Next

If mysign=-1 Then c10to2="-" & c10to2

End Function

%>

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=gb2312" />

<title>字符编码測试</title>

</head>

<style type="text/css">

body{ margin:20px 10px; line-height: 140%; font-size:12px; color:blue;}

</style>

<body>

<%

On Error Resume Next

str = "##testingTest$$##编码aa測aa试aa##!!67&#=;"

Response.Write("源字符串: " & str & "<BR>")

str1 = VB_URLEncode(str)

str2 = VB_URLDecode(str1)

Response.Write("VB_URLEncode: " & str1 & "<BR>")

Response.Write("VB_URLDecode: " & str2 & "<BR>")

If str2 = str Then Response.Write("结果==>解码正确, URLEncode对字符串中除26个英文字母(包含大写和小写)之外的全部字符都进行编码,中文字符为2字节,非中文字符1字节<BR>")

Response.Write("------------------------------------------------------- <BR>")

str3 = VB_GBtoUTF8(str)

str4 = VB_UTF8toGB(str3)

Response.Write("VB_GBtoUTF8: " & str3 & "<BR>")

Response.Write("VB_UTF8toGB: " & str4 & "<BR>")

If str4 = str Then Response.Write("结果==>解码正确,GBtoUTF8仅仅对中文字符编码,按每一个中文字符3字节编码<BR>")

Response.End()

%>

</body>

</html>

asp对中文编码及解码,Decode和Encode中文网址处理的更多相关文章

  1. python 编码与解码 decode解码 encode 编码

    >>> '无'   #gbk字符'\xce\xde'>>> str1 = '\xce\xde'>>> str1.decode('gbk')  # ...

  2. javascript处理HTML的Encode(转码)和解码(Decode)

    HTML的Encode(转码)和解码(Decode)在平时的开发中也是经常要处理的,在这里总结了使用javascript处理HTML的Encode(转码)和解码(Decode)的常用方式 一.用浏览器 ...

  3. Python3的decode()与encode()

    python3的decode()与encode() Tags: Python Python3 对于从python2.7过来的人,对python3的感受就是python3对文本以及二进制数据做了比较清晰 ...

  4. pyhton字符编码问题--decode和encode方法

    1  decode和encode方法 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成uni ...

  5. 【Python】关于decode和encode

    #-*-coding:utf-8 import sys ''' *首先要搞清楚,字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码, 即先将 ...

  6. python中decode和encode的区别

    #-*-coding:utf-8 import sys ''' *首先要搞清楚,字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码, 即先将 ...

  7. python编码问题 decode与encode

    参考: http://www.jb51.net/article/17560.htm 如果要在python2的py文件里面写中文,则必须要添加一行声明文件编码的注释,否则python2会默认使用ASCI ...

  8. ExtJs中decode与encode(转载)

    出自:http://blog.163.com/xiao_mege/blog/static/72942753201102693545195/ 在述说这个例子之前,我假想你已经知道什么是Json数据了,那 ...

  9. python decode和encode

    摘抄: 字符串在Python内部的表示是Unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符解码(decode)成unicode,再从unicode编码 ...

随机推荐

  1. 九度oj 题目1086:最小花费

    题目描述: 在某条线路上有N个火车站,有三种距离的路程,L1,L2,L3,对应的价格为C1,C2,C3.其对应关系如下: 距离s           票价 0<S<=L1         ...

  2. 【bzoj2597】[Wc2007]剪刀石头布 动态加边费用流

    题目描述 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之为剪刀石头布情况.有的时候,无聊的人们会津津乐道于统计有多少这 ...

  3. [BZOJ1582] [Usaco2009 Hol]Holiday Painting 节日画画(线段树)

    传送门 线段树区间修改傻题 #include <cstdio> #include <cstring> #include <iostream> #define N 5 ...

  4. Java面试题集(一)

    作为一名java开发软件工程,一定要记住,基础非常重要,往往就是一些基础,很简单,但是你就是不知道实现原理,为什么使用,有没有自己去发现,对比,差异从而总结,有些东西看似简单,但是不一定你描述清楚,直 ...

  5. PXC小结

    PXC使用到的端口号 3306 数据库对外服务的端口号(视具体情况而定) 4444 请求SST SST: 指数据一个镜象传输 xtrabackup , rsync ,mysqldump  4567 : ...

  6. hdu1671 Phone List [字典树 hash]

    传送门 Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. Yii2之创建定时任务

    yii开发的项目需要使用定时任务其实也可以使用一些单独的脚本文件来完成,但若是定时任务代码中需要使用到项目中的一些类,特别是需要使用应用对象Yii::$app的时候,单独的脚本想要完成就比较麻烦了.这 ...

  8. Java 并发编程中的 CountDownLatch 锁用于多个线程同时开始运行或主线程等待子线程结束

    Java 5 开始引入的 Concurrent 并发软件包里面的 CountDownLatch 其实可以把它看作一个计数器,只不过这个计数器的操作是原子操作,同时只能有一个线程去操作这个计数器,也就是 ...

  9. Java Socket应用

    Java Socket(套接字)通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络发出请求或者应答网络请求.

  10. Set 技巧之一

    我们知道set中 用set<int,int>S; S.lower_bound(x): 查找Set中 第一个>=x的数,返回结果是指针. S.upper_bound(x):查找Set中 ...