惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
博客园_首页
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
美团技术团队
小众软件
小众软件
V
V2EX
博客园 - Franky
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
Attack and Defense Labs
Attack and Defense Labs
S
Security Affairs
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
T
The Exploit Database - CXSecurity.com
有赞技术团队
有赞技术团队
S
Schneier on Security
人人都是产品经理
人人都是产品经理
Security Archives - TechRepublic
Security Archives - TechRepublic
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
K
Kaspersky official blog
PCI Perspectives
PCI Perspectives
AI
AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
O
OpenAI News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Register - Security
The Register - Security
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
博客园 - 【当耐特】
C
Cisco Blogs
大猫的无限游戏
大猫的无限游戏
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
S
Securelist
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
L
LangChain Blog
SecWiki News
SecWiki News
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V2EX - 技术
V2EX - 技术
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
L
LINUX DO - 热门话题
Cisco Talos Blog
Cisco Talos Blog

博客园 - Waver

Tabbing problems in Firefox in Mac OS X [转] Data Flow Diagrams Tutorial [转] HTML Ampersand Character Codes 也谈完美跨域,JavaScript [转] 浏览器类型检测. (JavaScript) 【转】 Route命令使用详解[转] 使用塑料水瓶要当心 Extension Method for ENUM. Design Material Websites Summarization. - Waver How to cut through the complexity to find a solution. How to install flash player on Google Chrome browser? ADODB.Stream 对象的属性与方法 VB/VBScript读取和保存UTF-8文件方案 Perl和OLE Automation Perl Unicode全攻略 ASP+COM 组件开发 SQL高级语法 无法去掉“关闭高级语言服务”解决方案! GCT 英语单词全部核心词汇A-Z GCT 工硕英语词汇语法
VB6: How To Convert UTF-8 Byte Arrays into Unicode Strings (and vice versa)
Waver · 2008-08-28 · via 博客园 - Waver
This code sample shows how to convert UTF-8 byte sequences (aka code page 65001) into unicode strings and back again in Visual Basic 6 instead of .Net like most of the examples I could find. What's more, this sample does not use APIs, but instead, relies on the Stream object provided by the ADODB library. This may not be the most efficient way of doing it, but these functions can be easily ported to Classic ASP by dropping all the variable types.

For this code to work, you will need to add a reference to the Microsoft ActiveX Data Objects 2.5 Library later versions of this library will also work.

The first function converts a unicode string to a byte array:

' accept a byte array containing utf-8 data
' and convert it to a string
Public Function ConvertStringToUtf8Bytes(ByRef strText As String) As Byte()
Dim objStream As ADODB.Stream
Dim data() As Byte
' init stream
Set objStream = New ADODB.Stream
objStream.Charset = "utf-8"
objStream.Mode = adModeReadWrite
objStream.Type = adTypeText
objStream.Open
' write bytes into stream
objStream.WriteText strText
objStream.Flush
' rewind stream and read text
objStream.Position = 0
objStream.Type = adTypeBinary
objStream.Read 3 ' skip first 3 bytes as this is the utf-8 marker
data = objStream.Read()
' close up and return
objStream.Close
ConvertStringToUtf8Bytes = data
End Function

This second function does the opposite, converting a byte array into a unicode string:

' accept a byte array containing utf-8 data
' and convert it to a string
Public Function ConvertUtf8BytesToString(ByRef data() As Byte) As String
Dim objStream As ADODB.Stream
Dim strTmp As String
' init stream
Set objStream = New ADODB.Stream
objStream.Charset = "utf-8"
objStream.Mode = adModeReadWrite
objStream.Type = adTypeBinary
objStream.Open
' write bytes into stream
objStream.Write data
objStream.Flush
' rewind stream and read text
objStream.Position = 0
objStream.Type = adTypeText
strTmp = objStream.ReadText
' close up and return
objStream.Close
ConvertUtf8BytesToString = strTmp
End Function

This test method uses a function called DecodeBase64 which is defined in this article: Free, Easy and Quick Base64 Encoding and Decoding in Visual Basic.

Public Sub Main()
Dim strB64 As String
Dim data() As Byte
Dim strTmp As String
' define test data as base64 and decode to array of bytes
strB64 = "R3JlZXRpbmdzIGFuZCBTYWx1dGF0aW9uISAo4oKsKSBhbmQgc29"
strB64 = strB64 & "tZSBVcmR1OiDaqdix2KfahtuMINm+2Kfaqdiz2KrYp9mG24w="
data = DecodeBase64(strB64)
' convert from utf-8 to string
strTmp = ConvertUtf8BytesToString(data)
' convert back to bytes
data = ConvertStringToUtf8Bytes(strTmp)
End Sub

Please note that the VB6 IDE and the standard VB6 form controls have difficulty showing Unicode characters and will show exotic characters as '?????'

The code from this article can be download as a VB6 project here: NonHostile_VB6_Convert_UTF8.zip

Hope this helps :-)