0

So, I'm trying to print some Japanese characters. I tried every possible thing. What am I missing?

#include <windows.h>
#include <string>

template<typename T>
void printW(const T* text) {
    WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), text, std::char_traits<T>::length(text), 0, 0);
}

template<typename T>
void print(const T* text) {
    WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), text, std::char_traits<T>::length(text), 0, 0);
}

int main() 
{
    //const char* text = "こんにちは\n";
    const wchar_t* textL = L"こんにちは\n";
    const char16_t* textu = u"こんにちは\n";
    const char32_t* textU = U"こんにちは\n";

    //printW(text);
    printW(textL);
    printW(textu);
    printW(textU);
}
DeiDei
  • 10,205
  • 6
  • 55
  • 80

1 Answers1

-5

WinAPI not needed, also you're using the wrong types.

#include <iostream>
#include <string>

int main() 
{
    std::string text{u8"こんにちは"};
    std::cout << text;
}

Live example

If you need to use the WinAPI, minimal modification is needed:

#include <windows.h>
#include <string>

template<typename T>
void print(const T* text) {
    WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), text, std::char_traits<T>::length(text), 0, 0);
}

int main() 
{
    auto text = u8"こんにちは\n";
    print(text);
}  

disclaimer: not tested on actual windows machine

  • 1
    Windows doesn't support UTF8 – Barmak Shemirani May 10 '16 at 01:15
  • @BarmakShemirani: Windows does support UTF-8 (the entire .NET metadata is encoded using UTF-8, for example). The implementation for console codepage 65001 (UTF-8) has limitations. – IInspectable May 10 '16 at 07:31
  • @IInspectable I don't know what .NET does. I mean WinAPI does not support UTF-8 screen output, or UTF-8 keyboard input. You can manipulate UTF-8 or any other data and use UTF-16 conversion. Maybe some .NET stuff can work directly with UTF-8 – Barmak Shemirani May 10 '16 at 09:21
  • 1
    @BarmakShemirani: It's a far stretch to say that *"Windows doesn't support UTF8"*. It does, just not everywhere. Windows' native Unicode encoding is UTF-16LE, and whenever you access system services, that's the character encoding you need to use. However, functions like [WideCharToMultiByte](https://msdn.microsoft.com/en-us/library/windows/desktop/dd374130.aspx) have full support for UTF-8 (and that's what Microsoft's CLR implementation uses to en-/decode .NET metadata). – IInspectable May 10 '16 at 09:35
  • I would like either you to explain what `u8` does on MSVC, a C++11 compliant compiler. Explain to me how `u8` is remotely the same concept as `wchar_t` because it isn't. – uh oh somebody needs a pupper May 10 '16 at 11:58
  • MSVC 14 has full support for explicit UTF-8 encoded string literals, and the contents of your *text* variable holds a string encoded using UTF-8. Your suggested solution breaks, because you are then passing that variable to an API call that expects ANSI (codepage) encoding. Obviously, a string literal prefix and a data type are different concepts. Not sure, what point you were trying to express here. – IInspectable May 10 '16 at 16:28