gpt4 book ai didi

excel - 获取 charcode 值大于十六进制 `FFFF` 的 Unicode 字符

转载 作者:行者123 更新时间:2023-12-02 00:34:53 24 4
gpt4 key购买 nike

问题

ChrW charcode 参数是一个 Long,用于标识字符,但不允许值大于 65535(十六进制值 &HFFFF) -请参阅MS Help .

例如,各种符号和象形文字可以在 Unicode 十六进制 block 1F300-1F5FF 中找到。因此,我没有找到任何方法来表示打开或关闭的挂锁符号的 ►1F5121F513 的建议十六进制值正是在这个字符代码块中,当然ChrW(&H1F512)将导致无效的过程/参数调用。

最近的 answer发现一个可能不稳定的替代方案,涉及较低的字符代码(通过 ChrW(&HE1F7)ChrW(&HE1F6)),但我正在寻找一种获得更高字符码表示的方法。

问题

是否有一种系统方法可以通过 VBA 或解决方法来表达大于 FFFF 的十六进制代码块中的 Unicode 字符

最佳答案

像这样的东西应该可以工作。大多数代码不是我写的,但我知道要寻找什么。基本上将十六进制映射到等效的字节数组,然后取回字符串。

 Option Explicit

'Pulled from https://www.di-mgt.com.au/howto-convert-vba-unicode-to-utf8.html
''' Maps a character string to a UTF-16 (wide character) string
Private Declare PtrSafe Function MultiByteToWideChar Lib "kernel32" ( _
ByVal CodePage As Long, _
ByVal dwFlags As Long, _
ByVal lpMultiByteStr As LongPtr, _
ByVal cchMultiByte As Long, _
ByVal lpWideCharStr As LongPtr, _
ByVal cchWideChar As Long _
) As Long

' CodePage constant for UTF-8
Private Const CP_UTF8 = 65001

''' Return length of byte array or zero if uninitialized
Private Function BytesLength(abBytes() As Byte) As Long
' Trap error if array is uninitialized
On Error Resume Next
BytesLength = UBound(abBytes) - LBound(abBytes) + 1
End Function

''' Return VBA "Unicode" string from byte array encoded in UTF-8
Public Function Utf8BytesToString(abUtf8Array() As Byte) As String
Dim nBytes As Long
Dim nChars As Long
Dim strOut As String
Utf8BytesToString = ""
' Catch uninitialized input array
nBytes = BytesLength(abUtf8Array)
If nBytes <= 0 Then Exit Function
' Get number of characters in output string
nChars = MultiByteToWideChar(CP_UTF8, 0&, VarPtr(abUtf8Array(0)), nBytes, 0&, 0&)
' Dimension output buffer to receive string
strOut = String(nChars, 0)
nChars = MultiByteToWideChar(CP_UTF8, 0&, VarPtr(abUtf8Array(0)), nBytes, StrPtr(strOut), nChars)
Utf8BytesToString = Left$(strOut, nChars)
End Function

'Grabbed from https://stackoverflow.com/questions/28798759/how-convert-hex-string-into-byte-array-in-vb6
Private Function HexToBytes(ByVal HexString As String) As Byte()
'Quick and dirty hex String to Byte array. Accepts:
'
' "HH HH HH"
' "HHHHHH"
' "H HH H"
' "HH,HH, HH" and so on.

Dim Bytes() As Byte
Dim HexPos As Integer
Dim HexDigit As Integer
Dim BytePos As Integer
Dim Digits As Integer

ReDim Bytes(Len(HexString) \ 2) 'Initial estimate.
For HexPos = 1 To Len(HexString)
HexDigit = InStr("0123456789ABCDEF", _
UCase$(Mid$(HexString, HexPos, 1))) - 1
If HexDigit >= 0 Then
If BytePos > UBound(Bytes) Then
'Add some room, we'll add room for 4 more to decrease
'how often we end up doing this expensive step:
ReDim Preserve Bytes(UBound(Bytes) + 4)
End If
Bytes(BytePos) = Bytes(BytePos) * &H10 + HexDigit
Digits = Digits + 1
End If
If Digits = 2 Or HexDigit < 0 Then
If Digits > 0 Then BytePos = BytePos + 1
Digits = 0
End If
Next
If Digits = 0 Then BytePos = BytePos - 1
If BytePos < 0 Then
Bytes = "" 'Empty.
Else
ReDim Preserve Bytes(BytePos)
End If
HexToBytes = Bytes
End Function

调用示例

Public Sub ExampleLock()
Dim LockBytes() As Byte
LockBytes = HexToBytes("F0 9F 94 92") ' Lock Hex representation, found by -->http://www.ltg.ed.ac.uk/~richard/utf-8.cgi
Sheets(1).Range("A1").Value = Utf8BytesToString(LockBytes) ' Output
End Sub

这是输出到 A1 的内容。

Lock

关于excel - 获取 charcode 值大于十六进制 `FFFF` 的 Unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56008191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com