如何在 C++ 中将 ASCII 字符串转换为 UTF8 字符串?

发布于 2024-08-22 19:32:46 字数 67 浏览 15 评论 0原文

如何在 C++ 中将 ASCII std::string 转换为 UTF8 (Unicode) std::string?

How to convert an ASCII std::string to an UTF8 (Unicode) std::string in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一生独一 2024-08-29 19:32:46
std::string ASCIIToUTF8(std::string str) {
  return str;
}

每个 ASCII 字符在 UTF8 中都有相同的表示形式,因此无需转换。
当然,如果输入字符串使用扩展(8 位)ASCII 字符集,则答案会更复杂。

std::string ASCIIToUTF8(std::string str) {
  return str;
}

Every ASCII character has the same representation in UTF8, so there is nothing to convert.
Of course, if the input string uses an extended (8-bit) ASCII character set, the answer is more complex.

旧伤慢歌 2024-08-29 19:32:46

ASCII 是一种七位编码,同样映射到可以用 ASCII 表示的字符子集的 UTF-8 编码。

简而言之,无事可做。您的 ASCII 字符串已经是有效的 UTF-8。

ASCII is a seven-bit encoding and maps identically onto the UTF-8 encoding of the subset of characters that can be represented in ASCII.

In short, there is nothing to do. Your ASCII string is already valid UTF-8.

萌辣 2024-08-29 19:32:46

我假设 ASCII 指的是 CP1252 或其他 8 位字符集(ASCII 只有 7 位,它直接与 UTF-8 兼容,不需要转换)。标准 C++ 无法做到这一点。您需要 Glibmm、Qt、inov 或 WINAPI 来完成此操作。

I assume that by ASCII you mean CP1252 or other 8 bit character set (ASCII is only 7 bits and it is directly compatible with UTF-8, no conversion required). Standard C++ cannot do it. You need e.g. Glibmm, Qt, iconv or WINAPI to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文