0

Part 1 on encoding characters in C++ (by User123).

-> Go to the next post.

I am making an intermediate program, where I need to display some special character, which is in Czech named háček (or c with caron): č. It has Unicode code \u010D. But when I run the program, I get some other character (big R with caron, I think): Ř. I don't see exactly in the terminal.

I have read a lot of articles about UTF and Unicode. Does Visual Studio has different encoding as terminal? Is the computer confused?

My example code (but not real) is as follows:

#include <iostream>

int main() {
    std::cout << "\u010D";
}

I have this output:

Ř

I tried also with printf function, but the same result.

Is there any similarity between these two encodings?

        č        |       Ř
-------------------------------
   dec  |   hex  |  dec  |  hex
   269  | U+010D |  344  | U+0158
196 141 |  C4 8D |197 152| C5 98
 &#269; | &#x10D | &#344;| &#x158;

Thank you!

Community
  • 1
  • 1
User123
  • 476
  • 7
  • 22
  • Please show the actual code you are having trouble with. And show the actual output you are seeing. – Remy Lebeau Feb 29 '20 at 21:45
  • Try `std::wcout << L"\u010D";`; – Paul Sanders Feb 29 '20 at 21:52
  • @Paul Sanders And what does it do? I tried but there is just blank output. – User123 Feb 29 '20 at 21:57
  • `std::wcout` is Unicode aware. `std::cout` is not. But, the console might not handle Unicode output correctly, I'm not sure. – Paul Sanders Feb 29 '20 at 21:58
  • 1
    @PaulSanders "_`std::wcout` is Unicode aware_" is a bit rich. It does not translate unicode into the specific encoding you wish to have. UTF-8, UTF-16 and UTF-32 does not work out of the box with `wcout` on Windows. Perhaps `wcout` and UTF-32 works elsewhere, but not on Windows. `UTF-16LE` (or whatever it's narrowed down to is called) works reasonably well on Windows but nowhere else. – Ted Lyngmo Feb 29 '20 at 22:08
  • I get `č` on my machine here which is properly UTF-8 in the terminal. Maybe your terminal *isn't* UTF-8, but some other encoding. To narrow this down, try a series of characters, not just that one, to see if there's any pattern to the mismatches. – tadman Feb 29 '20 at 22:47

0 Answers0