【问题标题】:How to convert UTF-16 to UTF-8 using C++?如何使用 C++ 将 UTF-16 转换为 UTF-8?
【发布时间】:2019-05-15 20:30:25
【问题描述】:
  • 我已经认识 'codecvt'、'WideCharToMultiByte' 和其他人。

我使用韩语。例如。 '안녕하세요'。

It 消息可以插入普通字符串类。对吧?

但就我而言。如果我有文件 :: 'test.txt' {in :: '안녕하세요'}

并阅读'test.txt'和getline(),

(test.txt file read)
string temp;
getline(file pointer, temp);
cout<<temp;

现在我使用 cout。达达!消息已损坏!

我知道这是 WideCharacter 问题。所以我尝试了 MultiByteToWideChar 方法。

好的。效果很好。

但我不想要这个。

最后我想读取宽字符文件,并保存“字符串”变量。

所以,我问你。

“不更改消息”时如何将 UTF-16(宽字符/wstring)转换为 UTF-8(多字节/字符串)?

:: 我想要这种风格

wstring temp = "안녕하세요"

string temp2 = convert_to_string(temp);

->

string temp2 = "안녕하세요"

【问题讨论】:

标签: c++ string utf-8 utf-16 wstring


【解决方案1】:

正如评论中提到的,您可以查看Convert C++ std::string to UTF-16-LE encoded string 了解如何进行转换的代码。

但是假设你有wstring 来保存你的韩语字符串,你就避免了区分 UTF-16-LE 和 UTF-16-BE 的麻烦,你可以很容易地找到每个韩语字符的 Unicode 代码点细绳。因此,您的问题归结为找到任何代码点的 UTF-8 表示。这并不难,请参见https://www.rfc-editor.org/rfc/rfc3629 的第 3 页(也可参见维基百科https://en.wikipedia.org/wiki/UTF-8)。

示例代码在 Convert Unicode code points to UTF-8 and UTF-32

【讨论】:

    猜你喜欢
    • 2015-09-21
    • 2013-05-20
    • 2015-09-19
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 2010-10-19
    • 2012-06-30
    相关资源
    最近更新 更多