Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert GB2312 to Unicode in vbs

Shulou Source: shulou.com Published: 2022-06-03 07:46:36 10月02日 Update

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

Tags: Code code article garbled content utf-8 not much function most character string that is efficiency helplessness times more level knowledge program Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi MySQL Docker OPPO Reno