Sign-up....

老革命遇上新问题:散分

最近写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

[1971 byte] By [msdn] at [2007-8-15 10:02:35]
# 1 Re: 老革命遇上新问题:散分

做asp两年了也没做过这些东西,楼主是高人,帮顶

pwycctv at 2005-12-30 20:35:53 >
# 2 Re: 老革命遇上新问题:散分

Response.Charset = "gb2312"试试

wanglh100 at 2005-12-30 21:12:01 >
# 3 Re: 老革命遇上新问题:散分

给系统加装个转换工具吧 ?! 用它来转换更便捷吧?!

soft_2008 at 2005-12-30 23:43:48 >
# 4 Re: 老革命遇上新问题:散分

建議寫個DLL,用於對接收字元轉換.然後再送.

xtuwz at 2005-12-31 8:47:56 >
# 5 Re: 老革命遇上新问题:散分

晕,我已经把大部分程序都升级到ASP.NET了,主要是避免我对ASP不熟的弱点.但这是短信网关接口啊,就算要改,也只能在这个程序里重定向,那样就可以用.NET来解决.但既然遇上了这样的问题,我就是想求出怎样用ASP解决,而且我相信ASP能做到.

xtgmdpdc at 2005-12-31 9:57:12 >
# 6 Re: 老革命遇上新问题:散分

觉得用ADODB.Stream进行转换还是比较好些。但出现的那个问题真的没碰到过。

帮老革命顶一把。

KimSoft at 2005-12-31 11:08:11 >
# 7 Re: 老革命遇上新问题:散分

建议使用封包截取工具,把信息抓出来分析一下,看看是不是进行流转换时出错了,对方的短信网关有字符和字数限制吗,先确认一下接口的参数是否符合要求

soft_2008 at 2005-12-31 12:16:26 >
# 8 Re: 老革命遇上新问题:散分

接分,谢谢楼主,

顺祝楼主在新的一年里:

事少钱多离家近,

睡觉睡到自然醒,

数钱数到手抽筋!

^_^

Iamahappycat at 2006-1-2 14:01:56 >
# 9 Re: 老革命遇上新问题:散分

记得有第三方转换工具

楼主可以搜一下

allenjay2003 at 2006-1-2 15:20:51 >
# 10 Re: 老革命遇上新问题:散分

学习中.......

willcoke at 2006-1-2 18:29:17 >
# 11 Re: 老革命遇上新问题:散分

入门中。帮顶。

xuxing_126 at 2006-1-3 5:15:53 >

Web

All Classified