How do I print Unicode to the output console in C with Visual Studio?

非 Y 不嫁゛ 提交于 2021-02-08 19:45:12

问题


As the question says, do I have to do in order to print Unicode characters to the output console? And what settings do I have to use? Right now I have this code:

wchar_t* text = L"the 来";
wprintf(L"Text is %s.\n", text);
return EXIT_SUCCESS;

and it prints: Text is the ?.

I've tried to change the output console's font to MS Mincho, Lucida Console and a bunch of others but they still don't display the japanese character.

So, what do I have to do?


回答1:


This is code that works for me (VS2017) - project with Unicode enabled

#include <stdio.h>
#include <io.h>
#include <fcntl.h>

int main()
{
    _setmode(_fileno(stdout), _O_U16TEXT);
    wchar_t * test = L"the 来. Testing unicode -- English -- Ελληνικά -- Español." ;

    wprintf(L"%s\n", test);
}

This is console

After copying it to the Notepad++ I see the proper string

the 来. Testing unicode -- English -- Ελληνικά -- Español.

OS - Windows 7 English, Console font - Lucida Console

Edits based on comments

I tried to fix the above code to work with VS2019 on Windows 10 and best I could come up with is this

#include <stdio.h>
int main()
{
    const auto* test = L"the 来. Testing unicode -- English -- Ελληνικά -- Español.";

    wprintf(L"%s\n", test);
}

When run it "as is" I see

When it is run with console set to Lucida Console fond and UTF-8 encoding I see

As the answer to 来 character shown as empty rectangle - I suppose is the limitation of the font which does not contain all the Unicode gliphs

When text is copied from the last console to Notepad++ all characters are shown correctly




回答2:


A question mark usually means Windows was unable to convert the character to the destination codepage. In the console a hollow square means the Unicode character was received correctly but it could not be displayed because the console font does not support it or it is a complex script requiring Uniscribe which the console does not handle. You can copy the square and paste it in Notepad/Wordpad and it should display correctly.

The WriteConsoleW Windows function can display Unicode characters and works all the way back to Windows NT. It can only write to the console so you must use WriteFile instead when the output is redirected. GetConsoleMode fails on redirected handles.

You don't say which VS version you are using and things have changed over the years but Unicode output has been decent since VS2005 if you call _setmode(_fileno(stdout), _O_U16TEXT); early in main():

#include <stdio.h>
#include <io.h>
#include <fcntl.h>

int main()
{
    _setmode(_fileno(stdout), _O_U16TEXT); // Call this before writing anything

    wchar_t * test = L"the 来" ;
    wprintf(L"Text is %s.\n", test);
    return 0;
}

See also: Myth busting in the console




回答3:


The characters '来' may not be in your system character code page. You need to save the characters as utf-8.

in vs2013, I try this:

// save as utf-8
#pragma execution_character_set( "utf-8" )

#include <Windows.h>

char *s = "the 来";

int main(){
    // set console code page to utf-8
    SetConsoleOutputCP(65001);
    printf("%s\n",s);
    return 0;
}


来源:https://stackoverflow.com/questions/46512441/how-do-i-print-unicode-to-the-output-console-in-c-with-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!