When you write
cout << "\u2780";
The compiler will convert \ u2780 to the corresponding encoding of this character in the execution character set. This is probably UTF-8, and so the line ends with four bytes (three for the character, one for the null terminator).
If you want to generate a character at runtime, you need to somehow make at runtime the same conversion to UTF-8 as at compile time.
C ++ 11 provides a convenient wstring_convert template and codecvt codecs that can do this, however libstdC ++, the standard library implementation shipped with gcc, has not yet managed to implement them (starting with gcc 4.8). The following shows how to use these functions, but you need to either use another standard library implementation or wait until libstdC ++ can implement them.
#include <codecvt> int main() { char32_t base = U'\u2780'; std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert; std::cout << convert.to_bytes(base + 5) << '\n'; }
You can also use any other way to create UTF-8 that you have. For example, iconv, ICU, and manual use of preec C ++ 11 codecvt_byname faces will work. (I am not showing these examples because this code will be more complex than the simple code allowed by wstring_convert .)
An alternative that would work for a small number of characters would be to create an array of strings using literals.
char const *special_character[] = { "\u2780", "\u2781", "\u2782", "\u2783", "\u2784", "\u2785", "\u2786", "\u2787", "\u2788", "\u2789" }; std::cout << special_character[i] << '\n';
bames53
source share