简体   繁体   English

将Unicode值分配给char

[英]Assign Unicode values to char

My code includes a loop in which checks every character of a std::string , assign it to a char variable, which fails its assertion when -1 >= c >= 255 . 我的代码包含一个循环,其中检查std::string每个字符,并将其分配给char变量,当-1 >= c >= 255时,该变量将断言。

It is a method from a JSON parser class that it's not mine: 这是JSON解析器类中的方法,不是我的:

static std::string UnescapeJSONString(const std::string& str)
{
    std::string s = "";

    for (int i = 0; i < str.length(); i++)
    {
        char c = str[i]; // << HERE FAILS WHEN 'É' CHARACTER
        if ((c == '\\') && (i + 1 < str.length()))
        {
            int skip_ahead = 1;
            unsigned int hex;
            std::string hex_str;

            switch (str[i+1])
            {
                case '"' :  s.push_back('\"'); break;
                case '\\':  s.push_back('\\'); break;
                case '/' :  s.push_back('/'); break;
                case 't' :  s.push_back('\t'); break;
                case 'n' :  s.push_back('\n'); break;
                case 'r' :  s.push_back('\r'); break;
                case 'b' :  s.push_back('\b'); break;
                case 'f' :  s.push_back('\f'); break;
                case 'u' :  skip_ahead = 5;
                    hex_str = str.substr(i + 4, 2);
                    hex = (unsigned int)std::strtoul(hex_str.c_str(), nullptr, 16);
                    s.push_back((char)hex);
                    break;

                default: break;
            }

            i += skip_ahead;
        }
        else
            s.push_back(c);
    }

    return Trim(s);
}

How can I assign a Unicode value to a char? 如何将Unicode值分配给char? In this case the value is É , and the code is not ready to receive such a characters. 在这种情况下,值是É ,并且代码尚未准备好接收此类字符。

This is included into a dll library, and is giving me this error: 这包含在dll库中,并给了我这个错误:

在此处输入图片说明

The std::string doesn't use Unicode. std :: string不使用Unicode。 This is evident because there is a method c_str that lets you get a char array from the std::string. 这很明显,因为有一个方法c_str,它使您可以从std :: string获取一个char数组。

Answering your question, your test is wrong: 回答您的问题,您的测试是错误的:

-1 >= c && c >= 255

It should be: 它应该是:

-1 <= c && c <= 255

But you can't get c anywhere near 255 since the char is signed. 但是由于字符已签名,因此您无法在接近255的任何地方获得c。 If you want to get 255 out a char it needs to be 如果要从一个字符中获取255,则必须为

unsigned char

This will not let you reach -1 though. 但是,这不会让您达到-1。

Read about char*'s here: 在这里阅读有关char *的信息:

http://www.cplusplus.com/doc/tutorial/variables/ http://www.cplusplus.com/doc/tutorial/variables/

see char arrays here: 在这里查看char数组:

http://www.cplusplus.com/doc/tutorial/ntcs/ http://www.cplusplus.com/doc/tutorial/ntcs/

see std::string here: 在这里看到std :: string:

http://www.cplusplus.com/reference/string/string/ http://www.cplusplus.com/reference/string/string/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM