Code of converting GB2312 to unicode in vbs

Time:2019-12-6

Today, I wrote a program similar to the following:

Copy codeThe 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

But I found that the returned Chinese was all garbled. After a look, I found that Sina’s code was GB2312, Khan, and now it’s the era of UTF-8 code. ResponseText supports UTF-8 encoding very well, but if it is GB2312 encoding, it will return garbled code, sometimes even error. But I had to use the ResponseBody and transcode by myself.

Copy codeThe 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 we have to write a gb2312tonuunicode function ourselves, which is easy to implement with ADO:

Copy codeThe 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

In this way, the default Unicode encoding of VBS string is returned. However, I can’t display my VBS level by ADO, so I wrote another one according to the “algorithm”:

Copy codeThe code is as follows:
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

Unfortunately, the efficiency is too low. Let’s practice.
Original: http://demon.tw/programming/vbs-gb2312-unicode.html