字符编码转换
#include <codecvt>
#include <locale>
#include <iostream>
using namespace std;
int main()
{
wstring_convert<codecvt_utf8_utf16<wchar_t>> converter;
string str = "你好,世界!";
wstring wstr = converter.from_bytes(str);
for (auto c : wstr) {
cout << hex << c << ' ';
}
cout << endl;
return 0;
}
在上述代码中,我们将UTF-8编码的字符串转换为UTF-16编码的字符串,并使用for
循环遍历UTF-16编码的字符串中的每个字符,并输出其16进制值。