asp中有关字符编码转换的几个函数
1
<%
2
1、'UTF转GB---将UTF8编码文字转换为GB编码文字
3
function UTF2GB(UTFStr)
4
5
for Dig=1 to len(UTFStr)
6
'如果UTF8编码文字以%开头则进行转换
7
if mid(UTFStr,Dig,1)="%" then
8
'UTF8编码文字大于8则转换为汉字
9
if len(UTFStr) >= Dig+8 then
10
GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
11
Dig=Dig+8
12
else
13
GBStr=GBStr & mid(UTFStr,Dig,1)
14
end if
15
else
16
GBStr=GBStr & mid(UTFStr,Dig,1)
17
end if
18
next
19
UTF2GB=GBStr
20
end function
21
22
'UTF8编码文字将转换为汉字
23
function ConvChinese(x)
24
A=split(mid(x,2),"%")
25
i=0
26
j=0
27
for i=0 to ubound(A)
28
A(i)=c16to2(A(i))
29
next
30
for i=0 to ubound(A)-1
31
DigS=instr(A(i),"0")
32
Unicode=""
33
for j=1 to DigS-1
34
if j=1 then
35
A(i)=right(A(i),len(A(i))-DigS)
36
Unicode=Unicode & A(i)
37
else
38
i=i+1
39
A(i)=right(A(i),len(A(i))-2)
40
Unicode=Unicode & A(i)
41
end if
42
next
43
44
if len(c2to16(Unicode))=4 then
45
ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))
46
else
47
ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))
48
end if
49
next
50
end function
51
52
'二进制代码转换为十六进制代码
53
function c2to16(x)
54
i=1
55
for i=1 to len(x) step 4
56
c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
57
next
58
end function
59
60
'二进制代码转换为十进制代码
61
function c2to10(x)
62
c2to10=0
63
if x="0" then exit function
64
i=0
65
for i= 0 to len(x) -1
66
if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
67
next
68
end function
69
70
'十六进制代码转换为二进制代码
71
function c16to2(x)
72
i=0
73
for i=1 to len(trim(x))
74
tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
75
do while len(tempstr)<4
76
tempstr="0" & tempstr
77
loop
78
c16to2=c16to2 & tempstr
79
next
80
end function
81
82
'十进制代码转换为二进制代码
83
function c10to2(x)
84
mysign=sgn(x)
85
x=abs(x)
86
DigS=1
87
do
88
if x<2^DigS then
89
exit do
90
else
91
DigS=DigS+1
92
end if
93
loop
94
tempnum=x
95
96
i=0
97
for i=DigS to 1 step-1
98
if tempnum>=2^(i-1) then
99
tempnum=tempnum-2^(i-1)
100
c10to2=c10to2 & "1"
101
else
102
c10to2=c10to2 & "0"
103
end if
104
next
105
if mysign=-1 then c10to2="-" & c10to2
106
end function
107
108
2、'GB转UTF8--将GB编码文字转换为UTF8编码文字
109
110
Function toUTF8(szInput)
111
Dim wch, uch, szRet
112
Dim x
113
Dim nAsc, nAsc2, nAsc3
114
'如果输入参数为空,则退出函数
115
If szInput = "" Then
116
toUTF8 = szInput
117
Exit Function
118
End If
119
'开始转换
120
For x = 1 To Len(szInput)
121
'利用mid函数分拆GB编码文字
122
wch = Mid(szInput, x, 1)
123
'利用ascW函数返回每一个GB编码文字的Unicode字符代码
124
'注:asc函数返回的是ANSI 字符代码,注意区别
125
nAsc = AscW(wch)
126
If nAsc < 0 Then nAsc = nAsc + 65536
127
128
If (nAsc And &HFF80) = 0 Then
129
szRet = szRet & wch
130
Else
131
If (nAsc And &HF000) = 0 Then
132
uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
133
szRet = szRet & uch
134
Else
135
'GB编码文字的Unicode字符代码在0800 - FFFF之间采用三字节模版
136
uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _
137
Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _
138
Hex(nAsc And &H3F Or &H80)
139
szRet = szRet & uch
140
End If
141
End If
142
Next
143
144
toUTF8 = szRet
145
End Function
146
147
3、'GB转unicode---将GB编码文字转换为unicode编码文字
148
149
function chinese2unicode(Str)
150
dim i
151
dim Str_one
152
dim Str_unicode
153
if(isnull(Str)) then
154
exit function
155
end if
156
for i=1 to len(Str)
157
Str_one=Mid(Str,i,1)
158
Str_unicode=Str_unicode&chr(38)
159
Str_unicode=Str_unicode&chr(35)
160
Str_unicode=Str_unicode&chr(120)
161
Str_unicode=Str_unicode& Hex(ascw(Str_one))
162
Str_unicode=Str_unicode&chr(59)
163
next
164
chinese2unicode=Str_unicode
165
end function
166
167
4、'URL解码
168
Function URLDecode(enStr)
169
dim deStr
170
dim c,i,v
171
deStr=""
172
for i=1 to len(enStr)
173
c=Mid(enStr,i,1)
174
if c="%" then
175
v=eval("&h"+Mid(enStr,i+1,2))
176
if v<128 then
177
deStr=deStr&chr(v)
178
i=i+2
179
else
180
if isvalidhex(mid(enstr,i,3)) then
181
if isvalidhex(mid(enstr,i+3,3)) then
182
v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
183
deStr=deStr&chr(v)
184
i=i+5
185
else
186
v=eval("&h"+Mid(enStr,i+1,2)+cstr(hex(asc(Mid(enStr,i+3,1)))))
187
deStr=deStr&chr(v)
188
i=i+3
189
end if
190
else
191
destr=destr&c
192
end if
193
end if
194
else
195
if c="+" then
196
deStr=deStr&" "
197
else
198
deStr=deStr&c
199
end if
200
end if
201
next
202
URLDecode=deStr
203
end function
204
205
'判断是否为有效的十六进制代码
206
function isvalidhex(str)
207
dim c
208
isvalidhex=true
209
str=ucase(str)
210
if len(str)<>3 then isvalidhex=false:exit function
211
if left(str,1)<>"%" then isvalidhex=false:exit function
212
c=mid(str,2,1)
213
if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
214
c=mid(str,3,1)
215
if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
216
end function
217
%>
218
<%2
1、'UTF转GB---将UTF8编码文字转换为GB编码文字3
function UTF2GB(UTFStr) 4

5
for Dig=1 to len(UTFStr) 6
'如果UTF8编码文字以%开头则进行转换7
if mid(UTFStr,Dig,1)="%" then 8
'UTF8编码文字大于8则转换为汉字9
if len(UTFStr) >= Dig+8 then 10
GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9)) 11
Dig=Dig+8 12
else 13
GBStr=GBStr & mid(UTFStr,Dig,1) 14
end if 15
else 16
GBStr=GBStr & mid(UTFStr,Dig,1) 17
end if 18
next 19
UTF2GB=GBStr 20
end function 21

22
'UTF8编码文字将转换为汉字23
function ConvChinese(x) 24
A=split(mid(x,2),"%") 25
i=0 26
j=0 27
for i=0 to ubound(A) 28
A(i)=c16to2(A(i)) 29
next 30
for i=0 to ubound(A)-1 31
DigS=instr(A(i),"0") 32
Unicode="" 33
for j=1 to DigS-1 34
if j=1 then 35
A(i)=right(A(i),len(A(i))-DigS) 36
Unicode=Unicode & A(i) 37
else 38
i=i+1 39
A(i)=right(A(i),len(A(i))-2) 40
Unicode=Unicode & A(i) 41
end if 42
next 43

44
if len(c2to16(Unicode))=4 then 45
ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode))) 46
else 47
ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode))) 48
end if 49
next 50
end function 51

52
'二进制代码转换为十六进制代码53
function c2to16(x)54
i=1 55
for i=1 to len(x) step 4 56
c2to16=c2to16 & hex(c2to10(mid(x,i,4))) 57
next 58
end function 59

60
'二进制代码转换为十进制代码61
function c2to10(x)62
c2to10=0 63
if x="0" then exit function 64
i=0 65
for i= 0 to len(x) -1 66
if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i) 67
next 68
end function 69

70
'十六进制代码转换为二进制代码71
function c16to2(x) 72
i=0 73
for i=1 to len(trim(x)) 74
tempstr= c10to2(cint(int("&h" & mid(x,i,1)))) 75
do while len(tempstr)<4 76
tempstr="0" & tempstr 77
loop 78
c16to2=c16to2 & tempstr 79
next 80
end function 81

82
'十进制代码转换为二进制代码83
function c10to2(x) 84
mysign=sgn(x) 85
x=abs(x) 86
DigS=1 87
do 88
if x<2^DigS then 89
exit do 90
else 91
DigS=DigS+1 92
end if 93
loop 94
tempnum=x 95

96
i=0 97
for i=DigS to 1 step-1 98
if tempnum>=2^(i-1) then 99
tempnum=tempnum-2^(i-1) 100
c10to2=c10to2 & "1" 101
else 102
c10to2=c10to2 & "0" 103
end if 104
next 105
if mysign=-1 then c10to2="-" & c10to2 106
end function107

108
2、'GB转UTF8--将GB编码文字转换为UTF8编码文字109

110
Function toUTF8(szInput)111
Dim wch, uch, szRet112
Dim x113
Dim nAsc, nAsc2, nAsc3114
'如果输入参数为空,则退出函数115
If szInput = "" Then116
toUTF8 = szInput117
Exit Function118
End If119
'开始转换120
For x = 1 To Len(szInput)121
'利用mid函数分拆GB编码文字122
wch = Mid(szInput, x, 1)123
'利用ascW函数返回每一个GB编码文字的Unicode字符代码124
'注:asc函数返回的是ANSI 字符代码,注意区别125
nAsc = AscW(wch)126
If nAsc < 0 Then nAsc = nAsc + 65536127
128
If (nAsc And &HFF80) = 0 Then129
szRet = szRet & wch130
Else131
If (nAsc And &HF000) = 0 Then132
uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)133
szRet = szRet & uch134
Else135
'GB编码文字的Unicode字符代码在0800 - FFFF之间采用三字节模版136
uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _137
Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _138
Hex(nAsc And &H3F Or &H80)139
szRet = szRet & uch140
End If141
End If142
Next143
144
toUTF8 = szRet145
End Function146

147
3、'GB转unicode---将GB编码文字转换为unicode编码文字148

149
function chinese2unicode(Str) 150
dim i 151
dim Str_one 152
dim Str_unicode 153
if(isnull(Str)) then154
exit function155
end if156
for i=1 to len(Str) 157
Str_one=Mid(Str,i,1) 158
Str_unicode=Str_unicode&chr(38) 159
Str_unicode=Str_unicode&chr(35) 160
Str_unicode=Str_unicode&chr(120) 161
Str_unicode=Str_unicode& Hex(ascw(Str_one)) 162
Str_unicode=Str_unicode&chr(59) 163
next 164
chinese2unicode=Str_unicode 165
end function 166

167
4、'URL解码168
Function URLDecode(enStr)169
dim deStr170
dim c,i,v171
deStr=""172
for i=1 to len(enStr)173
c=Mid(enStr,i,1)174
if c="%" then175
v=eval("&h"+Mid(enStr,i+1,2))176
if v<128 then177
deStr=deStr&chr(v)178
i=i+2179
else180
if isvalidhex(mid(enstr,i,3)) then181
if isvalidhex(mid(enstr,i+3,3)) then182
v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))183
deStr=deStr&chr(v)184
i=i+5185
else186
v=eval("&h"+Mid(enStr,i+1,2)+cstr(hex(asc(Mid(enStr,i+3,1)))))187
deStr=deStr&chr(v)188
i=i+3 189
end if 190
else 191
destr=destr&c192
end if193
end if194
else195
if c="+" then196
deStr=deStr&" "197
else198
deStr=deStr&c199
end if200
end if201
next202
URLDecode=deStr203
end function204

205
'判断是否为有效的十六进制代码206
function isvalidhex(str)207
dim c208
isvalidhex=true209
str=ucase(str)210
if len(str)<>3 then isvalidhex=false:exit function211
if left(str,1)<>"%" then isvalidhex=false:exit function212
c=mid(str,2,1)213
if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function214
c=mid(str,3,1)215
if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function216
end function217
%>218

我来自:向东博客



浙公网安备 33010602011771号