Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;从字符串生成unicode字符_C++_String_Utf 8_Char - Fatal编程技术网

C++ c++;从字符串生成unicode字符

C++ c++;从字符串生成unicode字符,c++,string,utf-8,char,C++,String,Utf 8,Char,我有一根这样的绳子 string s = "0081"; string c = "\u0081" 我需要制作一个这样的单字符字符串 string s = "0081"; string c = "\u0081" 如何从原来的长度为4的字符串生成长度为1的字符串 编辑: 我的错误,“\u0081”不是字符(1字节),而是2字节的字符/字符串? 所以我的输入是一个二进制,10000001,也就是0x81,这就是我的字符串“0081”的来源。 从这个0x81转到字符串c=“\u0081”

我有一根这样的绳子

string s = "0081";
string c = "\u0081"  
我需要制作一个这样的单字符字符串

string s = "0081";
string c = "\u0081"  
如何从原来的长度为4的字符串生成长度为1的字符串

编辑: 我的错误,“\u0081”不是字符(1字节),而是2字节的字符/字符串? 所以我的输入是一个二进制,10000001,也就是0x81,这就是我的字符串“0081”的来源。 从这个0x81转到字符串c=“\u0081”会更容易吗?该值是什么? 感谢所有的帮助

给你:

unsigned int x;
std::stringstream ss;
ss << std::hex << "1081";
ss >> x;

wchar_t wc1 = x;
wchar_t wc2 = L'\u1081';

assert(wc1 == wc2);

std::wstring ws(1, wc);
无符号整数x;
std::stringstream-ss;
ssx;
wchar_t wc1=x;
wchar_t wc2=L'\u1081';
断言(wc1==wc2);
标准::wstring ws(1,wc);

以下是整个过程,基于我在其他地方的评论中链接到的一些代码

string s = "0081";
long codepoint = strtol(s.c_str(), NULL, 16);
string c = CodepointToUTF8(codepoint);

std::string CodepointToUTF8(long codepoint)
{
    std::string out;
    if (codepoint <= 0x7f)
        out.append(1, static_cast<char>(codepoint));
    else if (codepoint <= 0x7ff)
    {
        out.append(1, static_cast<char>(0xc0 | ((codepoint >> 6) & 0x1f)));
        out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
    }
    else if (codepoint <= 0xffff)
    {
        out.append(1, static_cast<char>(0xe0 | ((codepoint >> 12) & 0x0f)));
        out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
        out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
    }
    else
    {
        out.append(1, static_cast<char>(0xf0 | ((codepoint >> 18) & 0x07)));
        out.append(1, static_cast<char>(0x80 | ((codepoint >> 12) & 0x3f)));
        out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
        out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
    }
    return out;
}
string s=“0081”;
长码点=strtol(s.c_str(),NULL,16);
字符串c=代码点TOUTF8(代码点);
std::字符串代码点toutf8(长代码点)
{
std::字符串输出;
如果(代码点6)&0x1f));
out.append(1,静态|ucast(0x80 |(codepoint&0x3f));
}
否则,如果(代码点>12)&0x0f));
out.append(1,静态|u转换(0x80 |)((代码点>>6)和0x3f));
out.append(1,静态|ucast(0x80 |(codepoint&0x3f));
}
其他的
{
out.append(1,静态_cast(0xf0 |)((codepoint>>18)和0x07));
out.append(1,静态|u转换(0x80 |)((代码点>>12)和0x3f));
out.append(1,静态|u转换(0x80 |)((代码点>>6)和0x3f));
out.append(1,静态|ucast(0x80 |(codepoint&0x3f));
}
返回;
}

请注意,此代码不进行任何错误检查,因此,如果您向其传递一个无效的代码点,您将返回一个无效的字符串。

您是否尝试过完成此操作?你是怎么失败的?如果您使用字符串c=“\u”+“0081”,是否确定只需要更小的码点
0x10000
;您得到一个错误,这是一个不完整的通用字符名\u因为c是一个由1个字符组成的字符串,您是否尝试类似c.replace(0,1,“9”);您只需替换所有内容,并且不再有\uxxx,但只有“9”我无法从4个字符的字符串“0081”中定义一个字符(\u0081)。如果您没有,则会出现错误,无法向
常量字符添加两个指针。您尝试过
字符串c=“\u0081”
?我想你会发现它不是一个1字符的字符串。例如@MarkRansom:取决于您使用的“角色”定义。这使得unicode非常有趣。他想要UTF-8,所以没有香蕉。总之,你确定他不想要完整的unicode代码点吗?@Duplicator idk,我只是向他展示了如何“从原始长度为4的字符串中生成长度为1的字符串”。你没有向他展示如何生成长度为1的字符串。您制作了一个wchar\u t,它甚至与wstring不同。您可以从
wchar\u t
轻松创建
wstring
。使用strtol比使用std::stringstream:
std::wstring ws(1,wchar_t(strtol(“1081”,0,16))玩游戏更容易
。然而,问题是通过UTF-8中的暗示产生一个字符串。@rici:我只是将其视为围绕任何关于unicode的问题的标准混淆,因为有人不理解关键的区分。因此,他可能指的是1个unicode字符(无论他认为这是一个码点还是字符)。这是UTF-8。“\u1081”三字节序列
e1 82 81
(U+1081缅甸字母SHAN-HA,如果有人感兴趣)好吧,哇,这实际上工作得很好,我可以问一下我需要学习什么才能理解为什么这样工作?非常感谢帮助!@mlf你需要了解UTF-8是如何组合起来的: