如何将 ANSI 字符 (char) 转换为 Unicode 字符 (wchar_t),反之亦然?
如何将 ANSI 字符 (char) 转换为 Unicode 字符 (wchar_t),反之亦然?
有没有用于此目的的跨平台源代码?
How can I convert from ANSI character (char) to Unicode character (wchar_t) and vice versa?
Is there any cross-platform source code for this purpose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,在
中,您有mbstowcs()
和wcstombs()
。我之前发布过关于如何使用它的一些代码,也许这很有帮助。确保运行该函数两次,一次获取长度,一次进行实际转换。 (这里有一些关于函数含义的讨论。)而不是手动 char 数组,我可能更喜欢
std::vector
或std::vector
,想到 它。请注意,
wchar_t
与 Unicode 无关。如果您需要 Unicode,则需要使用单独的库(例如iconv()
)进一步从wchar_t
转换为 Unicode,并且不要使用wchar_t
code> 作为 Unicode 代码点的数据类型。相反,请在旧系统上使用uint32_t
或在现代系统上使用char32_t
。Yes, in
<cstdlib>
you havembstowcs()
andwcstombs()
.I've previously posted some code on how to use this, maybe that's helpful. Make sure you run the function twice, once to get the length and once to do the actual conversion. (Here's a little discussion of what the functions mean.) Instead of the manual char array, I would probably prefer a
std::vector<char>
orstd::vector<wchar_t>
, coming to think of it.Note that
wchar_t
has nothing to do with Unicode. If you need Unicode, you need to further convert fromwchar_t
to Unicode using a separate library (likeiconv()
), and don't usewchar_t
as the data type for Unicode codepoints. Instead, useuint32_t
on legacy systems orchar32_t
on modern ones.显然这是有效的,我不知道它是否总是有效或者是否是巧合,但我认为值得展示
:
Apparently this works, I don't know if it will always work or if it's a coincidence, but I thought it was worth showing:
prints
查看 ICU 和 iconv 如果您确实使用 Unicode 而不仅仅是 16 位字符。也就是说,Unicode 不仅仅处理单个字符,甚至不像普通的 wchar_t 那样处理 16 位字符。
Look at libraries like ICU and iconv if you really are using Unicode and not just 16 bit characters. That is Unicode does not just deal with single characters not even 16 bit ones as plain wchar_t does.