Windows UTF8 到 UTF16 [英] Windows UTF8 to UTF16

查看:34
本文介绍了Windows UTF8 到 UTF16的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 C++ 在带有 BOM 的 UTF8 和 UTF16 LE 之间进行转换,以使字符在 Windows 上正确输出,而无需更改终端的字体.

I've been trying to convert between UTF8 and UTF16 LE with BOM using C++ to make the characters output correctly on Windows, without having to change the font of the terminal.

我尝试更改代码页,但没有奏效.

I tried changing the code pages, but they didn't work.

我有两个问题,

  1. 如何将普通字符串转换为宽字符串?
  2. 创建一个 C++ 映射来将每个 unicode 字符映射到 Windows 代码页中的字符是不是一个坏主意?

例如

wcout << L"\u00A0" << endl;

当使用代码页 850 时,此代码在 Windows 上输出字母 á.如何将变量替换为 "\u00A0" 以转换普通字符串到 Windows 上的宽字符?

This code outputs the letter á on Windows when using Code page 850. How can I put a variable in place of the "\u00A0" to convert a normal string to a wide character on Windows?

我想要的是这个:

wcout << Lsome_variable << endl;

我意识到这不是有效的 C++ 语法,但有人知道我该怎么做吗?或者是否有更好的方法?

I realise it's not valid c++ syntax but does anyone know how I can do this? Or if there's a better way?

推荐答案

如评论中所述,标准库现在提供诸如 std::wstring_convert(以及该页面另请参阅"部分中的其他函数/类).

As noted in the comments, the standard library now provides things like std::wstring_convert (and other functions/classes in the See Also section of that page).

由于您使用的是 Windows,WinAPI 还具有转换功能.在这种情况下,您将寻找 MultiByteToWideChar 可用于将 UTF-8 转换为 UTF-16.

Since you're on Windows, the WinAPI also has conversion functions. In this case you would be looking for MultiByteToWideChar which can be used to convert from UTF-8 to UTF-16.

在这些选项之间,某些内容应该适合您的用例.一般来说,您永远不需要编写自己的转换图.

Between those options, something should fit your use case. Generally speaking, you should never need to write your own conversion map.

这篇关于Windows UTF8 到 UTF16的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆