老革命遇上新问题:散分
最近写ASP程序,遇上如下问题:
对方传来参数为UTF-8,接收GB2312返回.也就是说,我要接收UTF-8,发送GB2312.狂晕,对方是短信网关,不肯做任何改动.本人想了多种方法均未解决问题.
一,我写了urldecode()函数,再利用ADODB.Stream进行转换,但如果字数为偶数,正常;字数为奇数,出现丢码,百思不得其解.
二,我设置代码页(65001)和字符集(UTF-8),但要把发送内容转为GB2312时又出了问题.
三,试用UTF-8代码页接收和重定向到另一网页(ASPX)用.NET进行转换,但违反了设计架构.
现我贴上我写的代码,请大侠帮忙.
str=urldecode(request("abc"))
str=unicode2gb(str,"UTF-8","GB2312")
response.write(str)
Function URLDecode(enStr)
dim deStr
dim c,i,v
deStr="
leng=Len(enStr)
for i=1 to leng
c=Mid(enStr,i,1)
if c="%" Then
v=eval("&h"+Mid(enStr,i+1,2))
if v<128 Then
deStr=deStr&chr(v)
i=i+2
Else
if isvalidhex(mid(enstr,i,3)) Then
if isvalidhex(mid(enstr,i+3,3)) Then
pre=Mid(enStr,i+1,2)
nex=Mid(enStr,i+4,2)
If nex=" Then
nex="FE"
End If
v=eval("&h"+pre+nex)
deStr=deStr&chr(v)
i=i+5
Else
pre=Mid(enStr,i+1,2)
nex=Mid(enStr,i+3,1)
If nex="&" Then
nex="FE"
v=eval("&h"+pre+nex)
deStr=deStr&chr(v)&"&"
Else
nex=Cstr(Hex(Asc(nex)))
v=eval("&h"+pre+nex)
deStr=deStr&chr(v)
End If
i=i+3
end If
else
destr=destr&c
end If
end If
Else
if c="+" Then
deStr=deStr&" "
Else
deStr=deStr+cstr(c)
end If
end If
Next
URLDecode=deStr
end Function
function isvalidhex(str)
isvalidhex=True
If str=" Then
str="%FE"
End If
str=ucase(str)
if len(str)<>3 then isvalidhex=false:exit Function
if left(str,1)<>"%" then isvalidhex=false:exit Function
c=mid(str,2,1)
if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit Function
c=mid(str,3,1)
if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit Function
end Function
function unicode2gb(str,oldCharset,newCharset)
Set stream=CreateObject("ADODB.Stream")
stream.type=2
stream.Mode=0
stream.Open
stream.Charset=newCharset
stream.WriteText str
stream.Position= 0
stream.Type= 2
stream.Charset=oldCharset
s=stream.ReadText
stream.Close
unicode2gb=s
End Function

