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 String,ByVal lpUsedDefaultChar As Long) As Long
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long,ByVal lpMultiByteStr As Long,ByVal cchWideChar As Long) As Long
Private Const CP_UTF8 = 65001
‘utf8转unicode
Function Utf8ToUnicode(ByRef Utf() As Byte) As String
Dim lRet As Long
Dim lLength As Long
Dim lBufferSize As Long
lLength = UBound(Utf) - LBound(Utf) + 1
If lLength <= 0 Then Exit Function
lBufferSize = lLength * 2
Utf8ToUnicode = String$(lBufferSize,Chr(0))
lRet = MultiByteToWideChar(CP_UTF8,VarPtr(Utf(0)),lLength,StrPtr(Utf8ToUnicode),lBufferSize)
If lRet <> 0 Then
Utf8ToUnicode = Left(Utf8ToUnicode,lRet)
End If
End Function
‘unicode转utf8 Function UnicodeToUtf8(ByVal UCS As String) As Byte() Dim lLength As Long Dim lBufferSize As Long Dim lResult As Long Dim abUTF8() As Byte lLength = Len(UCS) If lLength = 0 Then Exit Function lBufferSize = lLength * 3 + 1 ReDim abUTF8(lBufferSize - 1) lResult = WideCharToMultiByte(CP_UTF8,StrPtr(UCS),abUTF8(0),lBufferSize,vbNullString,0) If lResult <> 0 Then lResult = lResult - 1 ReDim Preserve abUTF8(lResult) UnicodeToUtf8 = abUTF8 End If End Function Private Sub Command1_Click() Dim byt() As Byte byt = UnicodeToUtf8("测试") Debug.Print Hex(byt(0)) & Hex(byt(1)) & Hex(byt(2)) Debug.Print Utf8ToUnicode(byt()) End Sub