我試圖理解 C unicode,現在讓我很困惑。
代碼:
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
using namespace std;
void trial1(){
string a = "\U00010000z";
cout << a << endl;
u16string b;
std::wstring_convert<codecvt_utf8<char16_t>, char16_t> converter;
b = converter.from_bytes(a);
u16string c = b.substr(0, 1);
string q = converter.to_bytes(c);
cout << q << endl;
}
void trial2(){
u16string a = u"\U00010000";
cout << a.length() << endl; // 2
std::wstring_convert<codecvt_utf8<char16_t>, char16_t> converter;
string b = converter.to_bytes(a);
}
int main() {
// both don't work
// trial1();
// trial2();
return 0;
}
我已經測驗過u16string可以將 unicode 存盤在 BMP 之外作為代理對,例如u"\U00010000"與 2 一起存盤char16_t。
那么為什么std::wstring_convert<codecvt_utf8<char16_t>, char16_t> converter;兩者都不起作用trial1并trial2拋出例外呢?
uj5u.com熱心網友回復:
std::codecvt_utf8不支持與 UTF-16 的轉換,僅支持 UCS-2 和 UTF-32。你需要std::codecvt_utf8_utf16改用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425944.html
上一篇:如何處理和遍歷這個串列?
