读书人

:UTF-8转汉字有关问题

发布时间: 2012-03-12 12:45:33 作者: rapoo

高手请进:UTF-8转汉字问题
本人做了一个网站流量分析系统,网页编码是:GB2312,现在想把UTF-8的编码转换成汉字,于是baidu了下面代码:

<%

function UTF2GB(UTFStr)
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 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

%>

<%
dim aa
aa= "%E7%94%B5%E8%84%91%E7%BB%B4%E4%BF%AE "
dim bb
bb=UTF2GB(aa)
%>

<%=bb%>

在使用中发现有如下问题:

1、当aa= "%E7%94%B5%E8%84%91%E7%BB%B4%E4%BF%AE "时,能够正常输出,显示结果:电脑维修;现象总结:当编码中全部为汉字时,能正确输出。

2、当aa= "%E7%94%B5%E8%84%91%2B%E7%BB%B4%E4%BF%AE ",却不能正常输出,浏览器报错:类型不匹配: '[string: "&H "] ' /001.asp, 第 55 行
正常的输出结果应该是:电脑+维修
现象总结:当编码中含有特殊符号时,不能正确输出。

本人所知道的特殊符号对应UTF-8编码有以下几个:

 字符       URL编码
  #        %23
  %        %25
  &        %26
  +        %2B
  \        %2F
  =        %3D
  ?        %3F

恳求:有哪位高手指点一下迷津,这些特殊符号应如何处理?谁有更好的代码?先谢拉!




[解决办法]
aa= "%E7%94%B5%E8%84%91%E7%BB%B4%E4%BF%AE ",电脑维修
aa= "%E7%94%B5%E8%84%91%2B%E7%BB%B4%E4%BF%AE ",电脑+维修

以上楼主提供的字符串,都不是utf-8,是unicode的字节流!

转换方式很简单,哪里有这么麻烦。

Function Str2Unicode(str)
Dim stm,ChrList
Dim i,j
Set stm=Server.CreateObject( "Adodb.Stream ")
stm.Type=2
stm.Mode=3
stm.Open
stm.Charset= "Unicode "
For i=0 To Len(str)/3-1
stm.WriteText Chrb(cint( "&H " & Mid(str,i*3+2,2)))

Next
stm.Position=0
stm.Type=2
stm.Charset= "utf-8 "
Str2Unicode=stm.ReadText(-1)
Set stm=Nothing
End Function
[解决办法]
不明白你的要求,我查了半天资料,是不是你弄混了?

比方 "电脑 维修 "

utf-8编码的urlencode是%E7%94%B5%E8%84%91%20%E7%BB%B4%E4%BF%AE,每个汉字3字节,ascii码1字节,全部%HEX方式显示,里面没有直接显示的字符,比方 " "或者 "+ "

gb2312编码的urlencode是%B5%E7%C4%D4+%CE%AC%D0%DE,每个汉字2字节%Hex,ascii码特殊字符替换,字母数字直接显示

具体不太清楚,还需要高人指教。

读书人网 >ASP

热点推荐