Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert GB2312 to Unicode in vbs

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report