现在位置:中国IDC吧>网页设计>JavaScript教程> 文章内容

Gb2312转utf-8(vbs js)

收藏发布 来源:互联网 作者:

昨天看了一下cocoon counter的代码,发现里面是用vbScript转的,费了以上午时间来研究,还是被搞得晕糊糊- -

他的vb转换函数是这样的:


Function DeCodeAnsi(s)
Dim i, sTmp, sResult, sTmp1
sResult = ""
For i=1 To Len(s)
If Mid(s,i,1)="%" Then
sTmp = "&H" & Mid(s,i 1,2)
If isNumeric(sTmp) Then
If CInt(sTmp)=0 Then
i = i 2
ElseIf CInt(sTmp)>0 And CInt(sTmp)<128 Then
sResult = sResult & Chr(sTmp)
i = i 2
Else
If Mid(s,i 3,1)="%" Then
sTmp1 = "&H" & Mid(s,i 4,2)
If isNumeric(sTmp1) Then
sResult = sResult & Chr(CInt(sTmp)*16*16 CInt(sTmp1))
i = i 5
End If
Else
sResult = sResult & Chr(sTmp)
i = i 2
End If
End If
Else
sResult = sResult & Mid(s,i,1)
End If
Else
sResult = sResult & Mid(s,i,1)
End If
Next
DeCodeAnsi = sResult
End Function

也就是用chr()函数把10进制的ANSI 字符代码转换成文字。文字本身应该是unicode,也就是vbs自动完成了gb-utf的转换,下面是我测试的一些数据:
测试代码:(需要把上面的代码加在前面)

<SCRIPT RUNAT=SERVER LANGUAGE=JAVASCRIPT>
Response.write("<br/>strx = chr(54992):");
Response.write(strx);
Response.write("<br/>strx.charCodeAt(0):");
Response.write(strx.charCodeAt(0));
Response.write("<br/>\"中\".charCodeAt(0):");
Response.write("中".charCodeAt(0));
Response.write("<br/>escape(strx):");
Response.write(escape(strx));
Response.write("<br/>encodeURI(strx):");
Response.write(encodeURI(strx));
Response.write("<br/>escape(\"中\"):");
Response.write(escape("中"));
Response.write("<br/>String.fromCharCode(20013):");
Response.write(String.fromCharCode(20013));
</SCRIPT>


分别调整文件存储格式,codepage,charset得到的结果:

文件为ansi格式:
codepage=936:
Response.Charset = "gb2312";
strx = chr(54992)
strx:中
strx.charCodeAt(0):20013
"中".charCodeAt(0):20013
escape(strx):%u4E2D
encodeURI(strx):涓

收藏此页到网摘/书签:
所有评论

评论列表

用户名: 新注册) 密码: 匿名评论