'代码出处:http://zhidao.baidu.com/link?url=q8JQLzay3Ng8DSXumGuUe9U4w4qmD3VUXSpftEFRdRCDtONCBhQcZVIXF5eA2cZFyO7kfl9EqpdMcSw5atv6qa
Private Sub Form_Load()
Dim a As String
Dim b() As Byte
Dim c As String
a = "123456abcdef"
b = StrConv(a, vbFromUnicode) '字符串转换为字节数组
c = StrConv(b, vbUnicode) '字节数组转换为字符串
MsgBox c
End Sub
'---------------------------------------------//方法一(默认编码)//---------------------------------------------
'代码出处:http://bbs.csdn.net/topics/390094323
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
Const cpUTF8 = 65001
Const cpGB2312 = 936
Const cpGB18030 = 54936
Const cpUTF7 = 65000
Function MultiByteToUTF16(UTF8() As Byte, CodePage As Long) As String '数组转文本(CodePage为常量中的编码)
Dim bufSize As Long
bufSize = MultiByteToWideChar(CodePage, 0&, UTF8(0), UBound(UTF8) + 1, 0, 0)
MultiByteToUTF16 = Space(bufSize)
MultiByteToWideChar CodePage, 0&, UTF8(0), UBound(UTF8) + 1, StrPtr(MultiByteToUTF16), bufSize
End Function
Function UTF16ToMultiByte(UTF16 As String, CodePage As Long) As Byte() '文本转数组(CodePage为常量中的编码)
Dim bufSize As Long
Dim arr() As Byte
bufSize = WideCharToMultiByte(CodePage, 0&, StrPtr(UTF16), Len(UTF16), 0, 0, 0, 0)
ReDim arr(bufSize - 1)
WideCharToMultiByte CodePage, 0&, StrPtr(UTF16), Len(UTF16), arr(0), bufSize, 0, 0
UTF16ToMultiByte = arr
End Function
'---------------------------------------------//方法二(自定义编码)//---------------------------------------------