2012-01-07 99 views
1

如果s包含20000jLen(s)以下如何将U + 20000之类的utf-32代码转换为VBA中的字符?

Dim b(1 To 8) 
b() = ChrW("&H" & Mid$(s, 1, j - 4)) & ChrW("&H" & Mid$(s, j - 3)) 

不起作用。它返回2个字符,而U + 20000是单个。

+1

这产生了两个字符,因为你_told_它产生两个字符:'CHRW $( “&H2”)CHRW $( “&H0000”)'。一个问题可能是'VBA是否支持utf-32' – 2012-01-07 21:34:45

回答

0

更好的代码是基于这里例如:http://www.vbforums.com/archive/index.php/t-526476.html

Public Function ChrU(UCode As String) As String 

Dim CharCode As Long 
CharCode = Val("&H00" & Right(UCode, Len(UCode) - 2)) 

If CharCode < 0 Then 
    CharCode = CharCode + 65536 
End If 

Dim lngChar As Long 
If CharCode >= 0 Then 
    If CharCode < &HD800& Then 
     ChrU = ChrW$(CharCode) 
     Exit Function 
    ElseIf CharCode < &HDC00& Then 
     ' UTF-16 surrogates are invalid in UTF-32 
    ElseIf CharCode < &HFFFF& Then 
     ChrU = ChrW$(CharCode) 
     Exit Function 
    ElseIf CharCode < &H10FFFF Then 
     lngChar = CharCode - &H10000 
     ChrU = ChrW$(&HD800& Or (lngChar \ 1024)) & ChrW$(&HDC00& Or (lngChar And &H3FF&)) 
     Exit Function 
    End If 
End If 
Err.Raise 5 
End Function 
+0

你应该发布这个作为你的问题的编辑,而不是作为答案。 – brettdj 2012-01-08 09:31:23

+0

很难说这是真的是编辑还是答案... – BoltClock 2012-01-08 11:45:09

+0

这是一个答案,代码适用于长六角形,我测试过并使用它。如果我发现一些错误,然后将它们发布在这里。 – Dims 2012-01-08 12:17:52

相关问题