VB代码:将字符串转换为UTF-8编码方式的字节数组
手头的工程需要使用SQLyogTunnel.php隧道对MYSQL数据库进行操作,为了保持最大程度的兼容,传输中始终使用BASE64对数据进行编码,数据库编码方式是utf-8。这样向该隧道发送数据时,就需要获取中文字符串的utf-8编码方式下的Base64编码。工程中,使用VB设计客户端程序与隧道通信,vb是微软旗下产品,使用unicode的编码方式。
明晨网络mingchennet.com发布过vb下的base64编码的类,这个类还可以计算字节数组的base64编码,所以如果将中文字符串转换为UTF-8编码方式的字节数组,就可以达到目的了。
做采集程序时,常常需要将UTF-8编码方式的网页内容转化为gb2312格式的字符串,实现这个功能的函数最有名的是BytesToBstr,使用了ADODB.Stream对象。借助ADODB.Stream,我们还可以将字符串转化为指定编码格式的字节数组。
- '========================================
- '函数名称:BytesToBstr
- '函数作用:将二进制数据根据指定编码格式转换为字符串
- '函数参数:内容,编码
- '函数说明:明晨网络 MingchenNet.com
- '========================================
- Public Function BytesToBstr(ByVal Body, ByVal Cset) As String
- On Error GoTo ErrHandle:
- Dim objStream As Object
- Dim sT As String
- Set objStream = CreateObject("ADODB.Stream")
- objStream.Type = 1
- objStream.Mode = 3
- objStream.Open
- objStream.Write Body
- objStream.Position = 0
- objStream.Type = 2
- objStream.CharSet = Cset
- sT = objStream.ReadText
- objStream.Close
- Set objStream = Nothing
- BytesToBstr = sT
- Exit Function
- ErrHandle:
- Set objStream = Nothing
- End Function
- '========================================
- '函数名称:Str2Byte
- '函数作用:将字符串转化为指定编码格式的数组
- '函数参数:内容,编码
- '函数说明:明晨网络 MingchenNet.com
- '========================================
- Public Function Str2Byte(ByVal Str As String, Optional ByVal CharSet As String = "utf-8") As Byte()
- On Error GoTo ErrHandle:
- Dim objStream As Object
- Dim tByte() As Byte
- Set objStream = CreateObject("ADODB.Stream")
- objStream.CharSet = CharSet
- objStream.Type = 2
- objStream.Mode = 3
- objStream.Open
- objStream.WriteText Str
- objStream.Flush
- objStream.Position = 0
- objStream.Type = 1
- objStream.Read 3
- tByte = objStream.Read()
- objStream.Close
- Set objStream = Nothing
- Str2Byte = tByte
- Exit Function
- ErrHandle:
- Set objStream = Nothing
- End Function
文章源自:明晨网络,明晨网络原创,VB代码:将字符串转换为UTF-8编码方式的字节数组,http://www.mingchennet.com
- 上一篇:VB代码:计算文件的HASH值,来自于emorcillo
- 下一篇:没有了