Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Having unicode console title..?

When I try to set my console's title to a string that has unicode characters in it, using SetConsoleTitle(), the title displays just some garbage characters instead.
I have also tried the SetConsoleTitleW() function, but that gives me the following error:

error: cannot convert 'const char*' to 'const WCHAR*' for argument '1' to 'BOOL SetConsoleTitleW(const WCHAR*)'

Any advice?


2 Answers

You have to use wide string literal, that is:

SetConsoleTitleW(L"DиD");

The L, before a quote denotes, that this is a wchar_t* string.

Also, for completness I have to say, that in C++11, there are new string literal prefixes defined:

const char a[] = u8"for a UTF-8 string.";
const char_16_t b[] = u"for a UTF-16 string.";
const char_32_t c[] = U"for a UTF-32 string.";

as usual wikipedia has more detailed note about that.

like image 88
Rafał Rawicki Avatar answered Nov 21 '25 18:11

Rafał Rawicki


It looks as if you are attempting to send UTF-8-encoded data to a function that expects UTF-16-encoded data.

You need to either convert the string literal to UTF-16 (i.e. WCHAR*) before passing it to the function, or create the literal as a WCHAR* literal (which I believe is done using the syntax L"DиD").

like image 22
Williham Totland Avatar answered Nov 21 '25 16:11

Williham Totland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!