可否用vba获得字符的utf8编码?

python中

print ('好'.encode("utf8"))
b'\xe5\xa5\xbd'

如何在vba中实现呢?

阅读 217
avatarAI BotBETA

在 VBA 中直接获取字符的 UTF-8 编码并不是一项内置的功能,因为 VBA 本身并不直接支持字节级别的操作或编码转换。不过,你可以通过一些方法间接实现这一点,例如使用 ActiveX 控件或调用 Windows API 函数。

以下是一个使用 Windows API 函数 MultiByteToWideCharWideCharToMultiByte 来实现字符到 UTF-8 编码转换的示例代码:

Declare PtrSafe Function MultiByteToWideChar Lib "kernel32" Alias "MultiByteToWideChar" (

ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As LongPtr, ByVal cbMultiByte As Long, 
ByVal lpWideCharStr As LongPtr, ByVal cchWideChar As Long) As Long

Declare PtrSafe Function WideCharToMultiByte Lib "kernel32" Alias "WideCharToMultiByte" (

ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As LongPtr, ByVal cchWideChar As Long, 
ByVal lpMultiByteStr As LongPtr, ByVal cbMultiByte As Long, ByVal lpDefaultChar As LongPtr, ByVal lpUsedDefaultChar As LongPtr) As Long

Function GetUTF8(ByVal InputStr As String) As String

Dim UTF8Bytes() As Byte
Dim WideCharStr() As WideString
Dim StrLen As Long
Dim i As Long

' Convert ANSI string to wide char string (Unicode)
StrLen = Len(InputStr)
ReDim WideCharStr(StrLen)
MultiByteToWideChar CP_ACP, 0, StrPtr(InputStr), StrLen, VarPtr(WideCharStr(0)), UBound(WideCharStr) + 1

' Convert wide char string to UTF-8 encoded byte array
ReDim UTF8Bytes(StrLen * 4) ' UTF-8 can be up to 4 bytes per character
WideCharToMultiByte CP_UTF8, 0, VarPtr(WideCharStr(0)), UBound(WideCharStr) + 1, VarPtr(UTF8Bytes(0)), UBound(UTF8Bytes) + 1, 0, 0

' Resize the byte array to the actual length of the UTF-8 string
ReDim Preserve UTF8Bytes(0 To WideCharToMultiByte(CP_UTF8, 0, VarPtr(WideCharStr(0)), UBound(WideCharStr) + 1, 0, 0, 0, 0) - 1)

' Convert byte array to VBA string (for demonstration purposes, we'll convert it to a hex string)
GetUTF8 = ""
For i = LBound(UTF8Bytes) To UBound(UTF8Bytes)
    GetUTF8 = GetUTF8 & Right("0" & Hex(UTF8Bytes(i)), 2)
Next i

End Function

Sub TestGetUTF8()

Dim Result As String
Result = GetUTF8("好")
Debug.Print Result  ' Output should be "E5A5BD"

End Sub


在这个示例中,`GetUTF8` 函数接受一个字符串输入,并将其转换为 UTF-8 编码的字节数组,最后将这些字节转换为十六进制字符串表示。`TestGetUTF8` 子程序演示了如何使用 `GetUTF8` 函数并输出结果。

请注意,这个示例使用了 `PtrSafe` 声明,这意味着它是为 64 位 Office 版本编写的。如果你使用的是 32 位 Office,可以去掉 `PtrSafe` 关键字。
1 个回答

可参考这个
How to convert VBA/VB6 Unicode strings to UTF-8

可以使用 Windows API 函数 WideCharToMultiByte。

Private Declare Function WideCharToMultiByte Lib "kernel32" ( _
    ByVal CodePage As Long, _
    ByVal dwFlags As Long, _
    ByVal lpWideCharStr As Long, _
    ByVal cchWideChar As Long, _
    ByVal lpMultiByteStr As Long, _
    ByVal cbMultiByte As Long, _
    ByVal lpDefaultChar As Long, _
    ByVal lpUsedDefaultChar As Long) As Long

Private Const CP_UTF8 As Long = 65001

Public Function Utf8BytesFromString(strInput As String) As Byte()
    Dim nBytes As Long
    Dim abBuffer() As Byte
    
    ' 获取转换后的字节数
    nBytes = WideCharToMultiByte(CP_UTF8, 0&, ByVal StrPtr(strInput), -1, 0&, 0&, 0&, 0&)
    
    ' 初始化字节数组
    ReDim abBuffer(nBytes - 1)
    
    ' 执行转换
    WideCharToMultiByte CP_UTF8, 0&, ByVal StrPtr(strInput), -1, ByVal VarPtr(abBuffer(0)), nBytes, 0&, 0&
    
    Utf8BytesFromString = abBuffer
End Function

Sub TestUtf8Encoding()
    Dim utf8Bytes() As Byte
    Dim i As Integer
    
    utf8Bytes = Utf8BytesFromString("好")
    
    ' 打印每个字节的十六进制表示
    For i = LBound(utf8Bytes) To UBound(utf8Bytes)
        Debug.Print Hex(utf8Bytes(i));
    Next i
End Sub

这个代码定义了一个函数 Utf8BytesFromString,它将 VBA 字符串转换为 UTF-8 编码的字节数组。

调用 TestUtf8Encoding 子程序后的输出结果:

E5 A5 BD

这表示字符串 “好” 被成功编码为UTF-8字节数组,其十六进制表示为 E5 A5 BD。与 Python 中的输出 b'\xe5\xa5\xbd' 是一致的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏