asp对中文编码及解码,Decode和Encode中文网址处理

<%‘--------------------------------------------------------------------------

‘==============================================================

‘功能: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="!""#$%&‘()*+,.-_/:;<=>[email protected][\]^`{|}~%"

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>

时间: 2024-07-30 20:24:17

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

python中编码和解码decode和encode的使用

python 在处理字符串时经常遇到编码错误,导致乱码,且python 2.x和 python 3.x之间有很大的不同,先在这里记录一下,以后整理; 转载 文章一篇: http://www.cnblogs.com/evening/archive/2012/04/19/2457440.html

python 编码与解码 decode解码 encode 编码

>>> '无'   #gbk字符'\xce\xde'>>> str1 = '\xce\xde'>>> str1.decode('gbk')  # 解码gbk为 unicodeu'\u65e0'>>> str1.decode('gbk').encode('utf-8') # 解码gbk为 unicode   编码unicode 为utf-8'\xe6\x97\xa0'>>> print str1.decode('gbk

decode()和encode()

Python中,我们使用decode()和encode()来进行解码和编码 在python中,使用unicode类型作为编码的基础类型.即 decode              encode str ---------> unicode --------->str u = u'中国' #显示指定unicode类型对象u str = u.encode('gb2312') #以gb2312编码对unicode对像进行编码 str1 = u.encode('gbk') #以gbk编码对unicod

ExtJs中decode与encode(转载)

出自:http://blog.163.com/xiao_mege/blog/static/72942753201102693545195/ 在述说这个例子之前,我假想你已经知道什么是Json数据了,那么在这里在温习一下吧: JSON(JavaScript Object Notation) 是一种数据交换格式,采用完全独立于语言的文本格式:JSON建构于两种结构:“名称/值”对的集合和值的有序列表 下面详细说明下:“名 称/值”对的集合(A collection of name/value pai

Python中decode与encode的区别

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

python decode unicode encode

字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码. 代码中字符串的默认编码与代码文件本身的编码一致,以下是不一致的两种: 1. s = u'你好' 该字符串的编码就被指定为unicode了,即python的内部编码,而与代码文件本身的编码(查看默认编码:import sys   print('hello',sys.getde

【Python】关于decode和encode

#-*-coding:utf-8 import sys ''' *首先要搞清楚,字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码, 即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码. decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码. encode的作

python 补充-decode和encode

1. decode与encode转码 在Python3中默认编码就是uncode,encode转成Byte类型 在Python2中默认编码就是ascii window下默认编码是GBK decode(告诉人家我是谁),encode(我要转成谁) s="你好" #uncode s_uncode=s.encode("utf-8").decode("utf-8") print(s_uncode) #uncode与utf-8的区别,uft-8节省了空间,

python 字符串编码 str和unicode 区别以及相互转化 decode(&#39;utf-8&#39;) encode(&#39;utf-8&#39;)

python 字符串编码 str和unicode 区别以及相互转化 decode('utf-8') encode('utf-8') 原文地址:https://www.cnblogs.com/zhaoyingjie/p/9133020.html