How to convert GB2312 to Unicode in vbs
Xiaobian to share with you how to convert GB2312 to Unicode in vbs, I believe most people still do not know how, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
Today I wrote a program similar to the following:
The code is as follows:
Dim http
Set http = CreateObject("msxml2.xmlhttp")
http.open "GET","http://www.sina.com.cn/",False
http.send
WScript.Echo http.responseText
However, I found that the returned Chinese are all garbled codes. I looked at it and found that the code of Sina is gb2312. Khan, it is now the era of utf-8 code. responseText supports utf-8 encoding very well, but if it is gb2312 encoding, it will return garbled code, and sometimes even report errors. Helpless, I had to use responseBody and then transcode myself.
The code is as follows:
Dim http
Set http = CreateObject("msxml2.xmlhttp")
http.open "GET","http://www.sina.com.cn/",False
http.send
WScript.Echo GB2312ToUnicode(http.responseBody)
So you have to write your own GB2312ToUnicode function, which is easy to implement with ado:
The code is as follows:
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
This way, the default Unicode encoding of the VBS string is returned. However, ado cannot display my VBS level, so I wrote another one according to the "algorithm":
The copy code is as follows:
Function GB2312ToUnicode(str)
length = LenB(str) : out = ""
For i = 1 To length
c = AscB(MidB(str,i,1))
If c