昨天看了一下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.