Gb2312转utf-8(vbs+js)

『本文地址:http://v3.djasp.net/Static/js/1943.stm

昨天看了一下cocoon counter的代码,发现里面是用vbScript转的,费了以上午时间来研究,还是被搞得晕糊糊。
他的vb转换函数是这样的:
★点击设计★ http://www.djasp.Net 全力打造WEB技术站点,欢迎大家访问!
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
本文由 ★点击设计★ http://www.djasp.Net 收集整理。谢绝无聊之人转载!
也就是用chr()函数把10进制的ANSI 字符代码转换成文字。文字本身应该是unicode,也就是vbs自动完成了gb-utf的转换,下面是我测试的一些数据:
测试代码:(需要把上面的代码加在前面)
此内容来源于 ★点击设计★ http://www.djasp.Net 网页编程资讯官方网站!
<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>
★点击设计★ http://www.djasp.Net 专业的WEB编程资讯技术站点,欢迎访问!

分别调整文件存储格式,codepage,charset得到的结果:
此内容来源于 ★点击设计★ http://www.djasp.Net 网页编程资讯官方网站!
文件为ansi格式:
codepage=936:
Response.Charset = "gb2312";
strx = chr(54992) 
strx:中
strx.charCodeAt(0):20013
"中".charCodeAt(0):20013
escape(strx):%u4E2D
encodeURI(strx):%E4%B8%AD
escape("中"):%u4E2D
String.fromCharCode(20013):中
盗版它人网站的内容可耻,您查看的内容来源于★点击设计★www.djasp.Net
Response.Charset = "utf-8";
strx = chr(54992)
strx:֐
strx.charCodeAt(0):20013
"֐".charCodeAt(0):20013
escape(strx):%u4E2D
encodeURI(strx):%E4%B8%AD
escape("֐"):%u4E2D
String.fromCharCode(20013):֐
本文由 点击设计 http://www.djasp.Net 收集整理。谢绝无聊之人转载!
codepage=65001:
Response.Charset = "gb2312";
strx = chr(54992)
strx:涓
strx.charCodeAt(0):20013
"".charCodeAt(0):-1.#IND
escape(strx):%u4E2D
encodeURI(strx):%E4%B8%AD
escape(""):
String.fromCharCode(20013):涓
本文由 点击设计 http://www.djasp.Net 收集整理。谢绝无聊之人转载!
Response.Charset = "utf-8";
strx = chr(54992)
strx:㝤
strx.charCodeAt(0):14180
"".charCodeAt(0):-1.#IND
escape(strx):%u3764
encodeURI(strx):%E3%9D%A4
escape(""):
String.fromCharCode(20013):中
本文由 点击设计 http://www.djasp.Net 收集整理。谢绝无聊之人转载!
文件为utf-8格式:
codepage=65001:
Response.Charset = "gb2312";
strx = chr(54992)
strx:涓
strx.charCodeAt(0):20013
"涓?.charCodeAt(0):20013
escape(strx):%u4E2D
encodeURI(strx):%E4%B8%AD
escape("涓?):%u4E2D
String.fromCharCode(20013):涓
请勿盗版 ★点击设计★ http://www.djasp.Net 网站上的内容,谢谢合作!
Response.Charset = "utf-8";
strx = chr(54992)
strx:中
strx.charCodeAt(0):20013
"中".charCodeAt(0):20013
escape(strx):%u4E2D
encodeURI(strx):%E4%B8%AD
escape("中"):%u4E2D
String.fromCharCode(20013):中
本文由 ★点击设计★ http://www.djasp.Net 收集整理。谢绝无聊之人转载!
codepage=936:
Active Server Pages 错误 ’ASP 0245’ 
代码页值的混合使用 
/referer_alapha/test2.asp,行 1 
指定的 @CODEPAGE 值与包括文件的 CODEPAGE 或文件的保存格式的值不一致。 
本文由 点击设计 http://www.djasp.Net 收集整理。谢绝无聊之人转载!
哈哈,是不是看晕了?我也晕,搞不明白为什么文件存储的格式跟chr(54992)这个函数怎么会扯上关系,而String.fromCharCode(20013)可以得到正确结果(测试的第四部分数据)。大概是Vbs里面逻辑太混乱了。
不管怎样,有了这个方法,gb2312转utf-8简单多了。

本文统计
上一篇: 经典正则表达式 (收藏整理)
下一篇: 制作圆角表格(Table)的完整代码
文章类别:常见问题经典收藏
最后更新:2006-9-23 10:50:29
浏览次数:
本栏最新文章
本栏推荐文章
随机酷站设计秀
网站导航 |走进点击 |点击作品 |服务项目 |联系我们 |设为首页 |加入收藏 |在线留言
点击设计
点击设计© Version: 2.3 WEB执行标准:W3C XHTML 1.1 / CSS 2.0 / Ajax
本站全面兼容 IE、FireFox、Netscape、Opera 等内核的浏览器
版权所有:点击设计 www.djasp.Net E-mail:djasp@qq.com
全程开发:秋水天子(Folier) QQ:39886616
版权所有:点击设计 www.djasp.net
浙ICP备05074939号