1

I am trying to print out the string 汉字 in Visual Studio 2019 C++. Something I think I should say is probably, I'm on Windows 10 and I have been trying to figure this out using the internet for an hour to no prevail.

This is my code:

#include <iostream>

int main(void) {
    std::wstring myWString = L"汉字"; // Tried a wstring, didn't output anything
    std::string myString = "汉字"; // Tried using a normal string, it outputted the �'s
    std::wcout << myWString << std::endl;
    std::cout << myString << std::endl;
}

In the projects properties, I added /source-charset:utf-8 /execution-charset:utf-8 to the additional options in Command Line under C/C++, and I also checked Beta: Use Unicode UTF-8 for worldwide language support. in my region settings. So why can't I print out these characters, and is this fixable, if so how?

Fixed: I went to my registry editor into HKEY_CURRENT_USER\Control Panel\Input Method, added a new value named EnableHexNumpad and set the data to '1'. After that I restarted my PC and it worked.

Zeeen
  • 312
  • 1
  • 2
  • 13
  • 1
    This might be because of the font used by the console. – 1201ProgramAlarm Oct 21 '20 at 03:09
  • You can't store `"汉字"` as-is into a `std::string`, you would need to encode it to UTF-8 first, eg: `u8"汉字"`. And your console would have to be configured to print out UTF-8. – Remy Lebeau Oct 21 '20 at 20:37
  • Well then I am not sure why storing 汉字 into a normal string then printing it out worked for me. – Zeeen Oct 24 '20 at 15:15

1 Answers1

-1

This question is similar to yours. Browse through the answers, there's one with printing out 你好. Output unicode strings in Windows console app (I would like to comment on your question, but my low reputation score does not allow me to do that, So I'm sorry to create an Answer comment.)

Joshua Ooi
  • 1,139
  • 7
  • 18
  • 1
    no, that's very old. New Windows 10 SDKs supports UTF-8 as a locale so no need to use UTF-16 like that anymore – phuclv Oct 21 '20 at 04:01
  • @phuclv except that you need to manually opt-in to that new SDK behavior, you don't get it for free. And that is assuming the new behavior is even enabled by the user to begin with. – Remy Lebeau Oct 21 '20 at 20:37
  • @RemyLebeau I know that. The OP already knows some UTF-8 related-options so just some more things will make it work – phuclv Oct 21 '20 at 23:46