Reputation: 3127
How can I make an arbitrary number be interpreted as Unicode when outputted to the terminal?
So for example:
#include <iostream>
int main() {
int euro_dec = 0x20AC;
std::cout << "from int: " << euro_dec
<< "\nfrom \\u: \u20AC" << std::endl;
return 0;
}
This prints:
from int: 8364
from \u: €
What does the escape sequence \u
do to make the number 0x20AC
be interpreted as Unicode?
I tested using wcout
and the output was:
from int: 8364
from \u:
Upvotes: 4
Views: 104
Reputation: 157414
A unicode escape sequence occurring in program text is converted to the equivalent Unicode character at the very first phase of translation (2.2p1b1 [lex.phases]). This occurs even before the program is tokenized or preprocessed.
To convert a Unicode codepoint expressed as an integer to your native narrow multibyte encoding, use c32rtomb
:
#include <cuchar>
char buf[MB_CUR_MAX];
std::mbstate_t ps{};
std::size_t ret = std::c32rtomb(buf, euro_dec, &ps);
if (ret != static_cast<std::size_t>(-1)) {
std::cout << std::string(buf, &buf[ret]); // outputs €
}
Note that cuchar
is poorly supported; if you know that your native narrow string encoding is UTF-8 you can use codecvt_utf8<char32_t>
but otherwise you'll have to use platform-specific facilities.
Upvotes: 3
Reputation: 409356
When you output the integer variable, the library converts the value to text, it doesn't actually output the value as an integer.
When using the "\u"
, it's the compiler which reads the number and converts it to the appropriate byte sequence which it inserts directly into the literal string.
Upvotes: 0