C++ Universal Unicodes -
i had quick question , couldn't find answer anywhere else. trying make universal function return proper unicode (instead of making literals) shown below in std::string getunicode() function. \xe2\x99\xa , cardtype being treated 2 separate strings in output, causes "?" followed cardtype number.
in case:
cout << "\xe2\x99\xa0"; //prints out symbol, cout << "\xe2\x99\xa" << 0; //prints out "?" followed 0. bad cout << card.getunicode(); //prints out "?" followed 0. bad
any ideas? 4-6 month beginner c++.
#ifndef card_h #define card_h #include <map> #include <sstream> #include <string> enum card_type {spade = 0, club = 3, heart = 5, diamond = 6}; class card { private: int number; card_type cardtype; public: card(card_type, int); void displaycard(); int getnumber() { return number; } card_type getcardtype() { return cardtype; } /* returns unicode value card type */ std::string getunicode() { std::stringstream ss; ss << "\xe2\x99\xa" << cardtype; return ss.str(); } }; #endif
this talked in c++ standard, section 2.14.5, paragraph 13:
[example:
"\xa" "b"
contains 2 characters
'\xa'
,'b'
after concatenation (and not single hexadecimal character'\xab'
). — end example ]
the problem '\xa'
being treated single character (hex value 0xa
10 in decimal, maps \n
(line feed) character in ascii/utf). cardtype
not "appended" escape sequence. in fact, escape sequence evaluated @ compile time, not runtime (which when card type gets evaluated).
in order work, need like:
ss << "\xe2\x99" << static_cast<char>(0xa0 + cardtype);
Comments
Post a Comment