简体   繁体   中英

How do I interpret a \u escape code in a C++ string?

I want to build a list of unicode glyphs programatically in C++, but I can't find a way to get around the compiler error \\u used with no following hex digits

#include <iostream>
#include <sstream>

using namespace std;

int main() 
{
    int unicode = 0x2200;
    stringstream result;
    for (int i = 0; i<0x0F; ++i)
    {
    unicode += i ;
    result << "\u" << hex << unicode << "\n";
    };

    cout << result.str();

    return 0;
}```



It is used to indicate Universal Character name. From this source we can see a more detailed description:

In character literals and native (non-raw) string literals, any character may be represented by a universal character name. Universal character names are formed by a prefix \\U followed by an eight-digit Unicode code point, or by a prefix \\u followed by a four digit Unicode code point. All eight or four digits, respectively, must be present to make a well-formed universal character name.

The error that you get is likely due to the absence of a variable value. I do not see where the hex is declared in provided context.

Thanks for the answers.

I did find a way to increment one glyph at a time ( I am referring to this list https://unicode-table.com/en/#arrows )

#include <iostream>
#include <sstream>

using namespace std;

int main() 
{
    std::string data = "\u2200";
    data[2] += 1;

    cout << "Starting glyph 2200-> \u2200 \n" 
    << "Expected glyph 2201-> \u2201 \n"
    << "Resulting glyph is-> " << data << '\n';

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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