关于编码的问题大家应该很了解了.但是在使用的使用不一定是按着我们自己的要求来实现编码.
使用有时需要转换一下.
extjs 是富客户端的UI库,在与服务器通讯的时候使用的是UTF-8的编码.最近写的网页代码使用的是GB2312的.使用要专业一下编码.负责在数据库里存放汉字的时候出现乱码.
extjs ajaxUTF-8编码转换
extjs默认使用UTF-8的编码,在服务器端我们经常使用GB2312之类编码.
在网上找了很多函数,经过测试修改.
实现了在服务器端编extjs ajaxUTF-8编码到GB2312编码的转变.
以下是用到的函数:
Function UTF2GB(UTFStr)
Dim Dig,GBStr
For Dig=1 to Len(UTFStr)
If mid(UTFStr,Dig,1)="%" Then
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
UTF2GB=GBStr
End Function
Function URLEncoding(vstrIn)
Dim strReturn,i,innerCode,ThisChr,Hight8,Low8
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
Function ConvChinese(x)
Dim A,i,j,DigS,Unicode
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)
Dim i
i = 1
For i = 1 To Len(x) Step 4
c2to16 = c2to16 & Hex(c2to10(Mid(x, i, 4)))
Next
End Function
Function c2to10(x)
Dim i
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)
Dim i,tempstr
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)
Dim mysign,DigS,tempnum,i
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
具体使用方法是:
Name=UTF2GB(URLEncodingTrim(Request.QueryString("name"))))
=========================================================================================
一下为各种编码的测试代码.
asp程序:
<%
Response.Addheader "Content-Type","text/html; charset=gb2312f"
Response.Write Server.URLEncode("测试用户")
Response.Write "<p>"
Dim s
s="%E6%B5%8B%E8%AF%95%E7%94%A8%E6%88%B7"
Dim d
d="测试用户"
'Response.Write c2Toutf(d)
'Response.Write ConvChinese(s)
Function c2Toutf(c2Str)
Dim i, temp
For i = 1 To LenB(c2Str)
temp = temp & CStr(MidB(c2Str, i, 1)) & "-"
Next
c2Toutf = temp
End Function
Function ConvChinese(x)
Dim A, i, j, DigS, Unicode
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)
Dim i
i = 1
For i = 1 To Len(x) Step 4
c2to16 = c2to16 & Hex(c2to10(Mid(x, i, 4)))
Next
End Function
Function c2to10(x)
Dim i
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)
Dim i, tempstr
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)
Dim i, DigS, mysign, tempnum
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
%>
版权声明:本文为博主原创文章,未经博主允许不得转载。
分享到:
相关推荐
深入浅出EXTJS241-320深入浅出EXTJS241-320深入浅出EXTJS241-320深入浅出EXTJS241-320深入浅出EXTJS241-320深入浅出EXTJS241-320深入浅出EXTJS241-320深入浅出EXTJS241-320深入浅出EXTJS241-320深入浅出EXTJS241-320...
使用ExtJs获取后台json格式的数据必须的七个jar包,commons-beanuti-1s-1.7.0.jar,commons-collections-3.1.jar,commons-lang-2.5.jar,commons-logging-1.0.4.jar,ezmorph-1.0.4.jar,json-lib-2.1.jar,...
ExtJS----HelloWorld程序源码
extjs2----关于extjs 的使用,操作.比较初级,但是也比较使用。适合初步接触extjs的朋友
extjs-basex.js extjs-basex.js extjs-basex.js
extjs--创建图表 折线图、饼状图、柱状图
EXTJS---完整数据库代码,全网唯一,非常适合EXTJS搭建框架,包含动态树,菜单,集成SPIRNGMVC+exjs+jdbc 等
一个extjs的OA项目 extjs-OA extjs-oaextjs-OA extjs-oa
extjs_4.1.0_community_extjs4-mvc-complex-dashboard
Extjs4---combobox省市区三级联动+struts2
Extjs4---grid的修改、删除功能---结合struts2、hibernate
深入浅出EXTJS241-320深入浅出EXTJS241-320深入浅出EXTJS241-320深入浅出EXTJS241-320
Extjs4--Form登录功能,结合struts2
extjs-620-docs官方文档extjs-620-docs官方文档extjs-620-docs官方文档
extjs4.2-2
ExtJS4.0-API Ext4.0-API Ext4 ExtJS4 API 学EXTJS4的好东西...
extJs-5.0.1-gpl附带sencha cmd安装程序、ruby编译包(分为32位和64位)和教程,一共四部分
extjs-theme-bootstrap-master.zip
extjs4-教程搭建 ExtJS 入门学习、可视化开发环境、布局详解 、文档阅读