昨天看了一下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簡單多了。
新聞熱點
疑難解答