C++中UTF-8字符串可用std::string存储,但size()返回字节数而非字符数,需用第三方库如ICU或std::wstring_convert进行字符级操作。

在C++中处理UTF-8编码的字符串,需要理解UTF-8是一种变长字符编码方式,一个中文字符通常占3到4个字节。标准库中的 std::string 可以存储UTF-8数据,但不能直接按“字符”操作(比如获取第n个汉字),因为 std::string::size() 返回的是字节数而非Unicode字符数。
std::string 本身不关心编码,可以安全地存储UTF-8文本:
std::string utf8_str = "你好,世界"; // 合法,UTF-8 编码 std::cout << utf8_str.size() << " bytes\n"; // 输出可能是 15(每个汉字3字节)
但不能用下标直接访问“第n个字符”,因为 utf8_str[0] 是第一个字节,不是第一个汉字。
要正确遍历每个Unicode字符,需解析UTF-8编码规则。可用循环或第三方库:
立即学习“C++免费学习笔记(深入)”;
#include <vector>
#include <cstdint>
<p>std::vector<uint32_t> utf8_to_codepoints(const std::string& str) {
std::vector<uint32_t> codepoints;
for (size_t i = 0; i < str.size();) {
uint8_t byte = str[i];
uint32_t codepoint;</p><pre class='brush:php;toolbar:false;'> if ((byte & 0x80) == 0) { // 0xxxxxxx: ASCII
codepoint = byte;
i += 1;
} else if ((byte & 0xE0) == 0xC0) { // 110xxxxx
codepoint = ((byte & 0x1F) << 6) | (str[i+1] & 0x3F);
i += 2;
} else if ((byte & 0xF0) == 0xE0) { // 1110xxxx
codepoint = ((byte & 0x0F) << 12) | ((str[i+1] & 0x3F) << 6) | (str[i+2] & 0x3F);
i += 3;
} else if ((byte & 0xF8) == 0xF0) { // 11110xxx
codepoint = ((byte & 0x07) << 18) | ((str[i+1] & 0x3F) << 12) | ((str[i+2] & 0x3F) << 6) | (str[i+3] & 0x3F);
i += 4;
} else {
// 处理错误字节(如无效UTF-8)
codepoint = 0xFFFD; // 替换符
i += 1;
}
codepoints.push_back(codepoint);
}
return codepoints;}
这样就可以得到每个Unicode码点,实现真正的“字符级”操作。
Windows平台常用 std::wstring 和 MultiByteToWideChar / WideCharToMultiByte 进行转换:
#ifdef _WIN32
#include <windows.h>
<p>std::wstring utf8_to_wstring(const std::string& utf8) {
int len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, nullptr, 0);
std::wstring wide(len, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &wide[0], len);
wide.pop_back(); // 移除末尾多余\0
return wide;
}</p><p>std::string wstring_to_utf8(const std::wstring& wstr) {
int len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
std::string utf8(len - 1, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &utf8[0], len, nullptr, nullptr);
return utf8;
}</p><h1>endif</h1>Linux/macOS推荐使用 ICU 或 Boost.Locale 库进行跨平台处理。
Boost.Locale 提供了简洁的UTF-8支持:
#include <boost/locale.hpp> #include <iostream> <p>std::string s = "Hello 世界"; std::cout << boost::locale::length(s) << " characters\n"; // 正确输出字符数</p><p>// 转大写(支持Unicode) std::string upper = boost::locale::to_upper(s, "zh_CN.UTF-8");</p>
安装Boost后编译时链接 -lboost_locale 即可。
总结: C++原生不提供完整UTF-8支持,std::string 可存储但不可直接按字符操作。手动解析码点适用于简单场景,复杂项目建议使用 Boost.Locale 或 ICU 库来处理编码转换、字符计数、大小写转换等操作。跨平台开发时尤其要注意系统默认编码差异。
基本上就这些。
以上就是c++++怎么处理UTF-8编码的字符串_c++ UTF-8字符串处理与转换方法的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号