首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么codecvt不能将BMP外部的unicode转换为u16string?

为什么codecvt不能将BMP外部的unicode转换为u16string?
EN

Stack Overflow用户
提问于 2022-02-14 07:15:43
回答 1查看 32关注 0票数 0

我正在努力理解C++ unicode,现在我对此感到困惑。

代码:

代码语言:javascript
复制
#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可以将u16string外部的unicode存储为代理项对,例如,u"\U00010000"与2 char16_t一起存储。

那么,为什么std::wstring_convert<codecvt_utf8<char16_t>, char16_t> converter;不能同时适用于trial1trial2并引发异常呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-14 08:10:39

std::codecvt_utf8不支持转换到或从UTF-16,只有UCS-2和UTF-32.您需要使用std::codecvt_utf8_utf16代替。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71108289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档