vbs中將GB2312轉(zhuǎn)Unicode的代碼
更新時(shí)間:2011年02月03日 15:33:00 作者:
現(xiàn)在都是utf-8編碼的時(shí)代了。responseText對(duì)utf-8編碼支持得很好,但是如果是gb2312編碼就會(huì)返回亂碼,有時(shí)甚至?xí)?bào)錯(cuò)。
今天寫(xiě)了一個(gè)類(lèi)似于下面的程序:
Dim http
Set http = CreateObject("msxml2.xmlhttp")
http.open "GET","http://www.sina.com.cn/",False
http.send
WScript.Echo http.responseText
但是卻發(fā)現(xiàn)返回的中文都是亂碼,看了一下發(fā)現(xiàn)新浪的編碼竟然是gb2312的,汗,現(xiàn)在都是utf-8編碼的時(shí)代了。responseText對(duì)utf-8編碼支持得很好,但是如果是gb2312編碼就會(huì)返回亂碼,有時(shí)甚至?xí)?bào)錯(cuò)。無(wú)奈,只好用responseBody然后自己轉(zhuǎn)碼。
Dim http
Set http = CreateObject("msxml2.xmlhttp")
http.open "GET","http://www.sina.com.cn/",False
http.send
WScript.Echo GB2312ToUnicode(http.responseBody)
于是就要自己寫(xiě)一個(gè)GB2312ToUnicode函數(shù),用ado很容易實(shí)現(xiàn):
Function GB2312ToUnicode(str)
With CreateObject("adodb.stream")
.Type = 1 : .Open
.Write str : .Position = 0
.Type = 2 : .Charset = "gb2312"
GB2312ToUnicode = .ReadText : .Close
End With
End Function
這樣返回的就是VBS字符串默認(rèn)的Unicode編碼了,不過(guò)用ado不能顯示我鬼使神差的VBS水平,于是自己根據(jù)“算法”再寫(xiě)了一個(gè):
Function GB2312ToUnicode(str)
length = LenB(str) : out = ""
For i = 1 To length
c = AscB(MidB(str,i,1))
If c <= 127 Then
out = out & Chr(c)
Else
i = i + 1
d = Hex(AscB(MidB(str,i,1)))
c = "&H" & Hex(c) & d
out = out & Chr(c)
End If
Next
GB2312ToUnicode = out
End Function
只可惜效率太低,就當(dāng)練練手吧。
原文:http://demon.tw/programming/vbs-gb2312-unicode.html
復(fù)制代碼 代碼如下:
Dim http
Set http = CreateObject("msxml2.xmlhttp")
http.open "GET","http://www.sina.com.cn/",False
http.send
WScript.Echo http.responseText
但是卻發(fā)現(xiàn)返回的中文都是亂碼,看了一下發(fā)現(xiàn)新浪的編碼竟然是gb2312的,汗,現(xiàn)在都是utf-8編碼的時(shí)代了。responseText對(duì)utf-8編碼支持得很好,但是如果是gb2312編碼就會(huì)返回亂碼,有時(shí)甚至?xí)?bào)錯(cuò)。無(wú)奈,只好用responseBody然后自己轉(zhuǎn)碼。
復(fù)制代碼 代碼如下:
Dim http
Set http = CreateObject("msxml2.xmlhttp")
http.open "GET","http://www.sina.com.cn/",False
http.send
WScript.Echo GB2312ToUnicode(http.responseBody)
于是就要自己寫(xiě)一個(gè)GB2312ToUnicode函數(shù),用ado很容易實(shí)現(xiàn):
復(fù)制代碼 代碼如下:
Function GB2312ToUnicode(str)
With CreateObject("adodb.stream")
.Type = 1 : .Open
.Write str : .Position = 0
.Type = 2 : .Charset = "gb2312"
GB2312ToUnicode = .ReadText : .Close
End With
End Function
這樣返回的就是VBS字符串默認(rèn)的Unicode編碼了,不過(guò)用ado不能顯示我鬼使神差的VBS水平,于是自己根據(jù)“算法”再寫(xiě)了一個(gè):
復(fù)制代碼 代碼如下:
Function GB2312ToUnicode(str)
length = LenB(str) : out = ""
For i = 1 To length
c = AscB(MidB(str,i,1))
If c <= 127 Then
out = out & Chr(c)
Else
i = i + 1
d = Hex(AscB(MidB(str,i,1)))
c = "&H" & Hex(c) & d
out = out & Chr(c)
End If
Next
GB2312ToUnicode = out
End Function
只可惜效率太低,就當(dāng)練練手吧。
原文:http://demon.tw/programming/vbs-gb2312-unicode.html
相關(guān)文章
VBS基礎(chǔ)篇 - vbscript TextStream對(duì)象
TextStream對(duì)象是用于訪問(wèn)文本文件的對(duì)象,它是FileSystemObject一個(gè)獨(dú)立的附屬對(duì)象,但在使用TextStream對(duì)象時(shí),我們?nèi)砸柚鶩ileSystemObject 對(duì)象或其附屬對(duì)象來(lái)創(chuàng)建一個(gè) TextStream 對(duì)象并訪問(wèn)磁盤(pán)文件的內(nèi)容,需要的朋友可以參考下2018-06-06遠(yuǎn)程開(kāi)啟/關(guān)閉目標(biāo)telnet服務(wù)的windows腳本RTCS.vbs
遠(yuǎn)程開(kāi)啟/關(guān)閉目標(biāo)telnet服務(wù)的windows腳本RTCS.vbs...2007-02-02在vbs運(yùn)行命令行工具后讓命令窗口保持打開(kāi)狀態(tài)的腳本
在vbs運(yùn)行命令行工具后讓命令窗口保持打開(kāi)狀態(tài)的腳本...2007-03-03用VBS模擬二叉樹(shù),可以得到一個(gè)排序辦法.
用VBS模擬二叉樹(shù),可以得到一個(gè)排序辦法....2007-03-03VBS教程:VBScript 語(yǔ)句-With 語(yǔ)句
VBS教程:VBScript 語(yǔ)句-With 語(yǔ)句...2006-11-11