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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218
