使用 C++11,我如何从 UTF-8 编码std::string
,将文本每个字符的Unicode值放入一个uint32_t
?
就像是:
void f(const std::string &utf8_str)
{
for(???) {
uint32_t code = ???;
/* Do my stuff with the code... */
}
}
假设主机系统区域设置是 UTF-8 有帮助吗? C++11 为该任务提供了哪些标准库工具?
您可以使用提供的转换方面简单地将字符串转换为 UTF-32 编码的字符串,然后std::wstring_convert http://en.cppreference.com/w/cpp/locale/wstring_convert from <locale>
:
#include <codecvt>
#include <locale>
#include <string>
void foo(std::string const & utf8str)
{
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
std::u32string utf32str = conv.from_bytes(utf8str);
for (char32_t u : utf32str) { /* ... */ }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)