国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Gb2312轉utf-8(vbs+js)

2024-07-21 02:25:14
字體:
來源:轉載
供稿:網友

昨天看了一下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):%e4%b8%ad
escape("中"):%u4e2d
string.fromcharcode(20013):中

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):֐

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):涓

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):中

文件為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):涓

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):中

codepage=936:
active server pages 錯誤 'asp 0245'
代碼頁值的混合使用
/referer_alapha/test2.asp,行 1
指定的 @codepage 值與包括文件的 codepage 或文件的保存格式的值不一致。


哈哈,是不是看暈了?我也暈,搞不明白為什么文件存儲的格式跟chr(54992)這個函數怎么會扯上關系,而string.fromcharcode(20013)可以得到正確結果(測試的第四部分數據)。大概是vbs里面邏輯太混亂了。
不管怎樣,有了這個方法,gb2312轉utf-8簡單多了。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 寿宁县| 巫山县| 安泽县| 伊川县| 磐安县| 江油市| 六盘水市| 富阳市| 安陆市| 马鞍山市| 九江市| 茶陵县| 会宁县| 东乡县| 资中县| 额尔古纳市| 永兴县| 拜城县| 宁德市| 夏津县| 鹤岗市| 子长县| 永春县| 郓城县| 汶上县| 信宜市| 繁昌县| 清苑县| 乐山市| 晋州市| 新邵县| 墨脱县| 黄浦区| 静海县| 宁波市| 西丰县| 荥经县| 永昌县| 内乡县| 穆棱市| 乐亭县|