Spej
Spej

Reputation: 31

How can I get a Unicode character from a number?

I'm new to C++, so sorry if this is obvious.

How can I get a character from a number? I have the following code:

for (int i = 0; i < 500; i++) {
    cout << i;
}

It's supposed to get the first 500 characters in the Unicode dictionary. I know that in javascript, it is String.fromCodePoint(i). What's the C++ equivalent?

Upvotes: 0

Views: 324

Answers (1)

phuclv
phuclv

Reputation: 41932

Use wchar_t instead

for (wchar_t i = 0; i < 500; i++) {
    wcout << i;
}

You can also use char16_t and char32_t if you're using C++11 or newer

However you still need a capable terminal and also need to set the correct codepage to get the expected output. On Linux it's quite straight forward but if you're using (an older) Windows it's much trickier. See Output unicode strings in Windows console app

Upvotes: 3

Related Questions