vba 在VB中获取字符的unicode值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4006246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the unicode value of a char in VB
提问by Senseful
How can I get the unicode value of a char?
如何获取字符的 unicode 值?
For example, I know that I can do this with ascii:
例如,我知道我可以用 ascii 做到这一点:
i = Asc("a") // i == 97 (correct)
What if I have a unicode char though?
如果我有一个 unicode 字符怎么办?
i = Asc("?") // i == 149 (incorrect... should return 8226)
Obviously the second example doesn't work since that character is not in the Ascii set. Is there an equivalent function that I can use which will return 8226instead of the incorrect result 149?
显然,第二个示例不起作用,因为该字符不在 Ascii 集中。是否有我可以使用的等效函数,它会返回8226而不是错误的结果149?
I'm doing this in Outlook 2003, if that makes any difference.
我在 Outlook 2003 中这样做,如果这有什么不同的话。
回答by Guillaume
The answer provided by RC. helped me a lot but I had issues with the AscW()function sometimes returning negative values.
RC 提供的答案。帮了我很多,但我AscW()有时会返回负值的函数有问题。
In my case the problem appeared when working with Chinese characters.
在我的情况下,使用中文字符时出现了问题。
I found a work around on the web:
我在网上找到了一个解决方法:
Function CharToUnicode(strChar As String)
Dim lngUnicode As Long
lngUnicode = AscW(strChar)
If lngUnicode < 0 Then
lngUnicode = 65536 + lngUnicode
End If
CharToUnicode = lngUnicode
End Function

