output utf8 in console with Visual Studio (wide stream)

戏子无情 提交于 2021-01-29 04:11:45

问题


This piece of code works if i compiled it with mingw32 on windows 10. and emits right result, as you can see below :

C:\prj\cd>bin\main.exe
1°à€3§4ç5@の,は,でした,象形字 ;

Indeed when i try to compile it with Visual Studio 17, same code emits wrong chracters

/out:prova.exe
prova.obj

C:\prj\cd>prova.exe
1°à€3§4ç5@ã®,ã¯,ã§ã—ãŸ,象形字 ;

C:\prj\cd>

here source code :

#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <string>
#include <iostream>

int main ( void )
{
    _wsetlocale(LC_ALL, L"it_IT.UTF-8" );   // set locale wide string
    _setmode(_fileno(stdout), _O_U8TEXT);   // set Locale for console
    SetConsoleCP( CP_UTF8 ) ;               
    SetConsoleOutputCP(CP_UTF8);

    // Enable buffering to prevent VS from chopping up UTF-8 byte sequences
    setvbuf(stdout, nullptr, _IOFBF, 1000);

    std::wstring test = L"1°à€3§4ç5@の,は,でした,象形字 ;";
    std::wcout << test << std::endl;

}

I have read several topics :

How to print UTF-8 strings to std::cout on Windows?

How to make std::wofstream write UTF-8?

and many others, but somehtins goes wrong ... can you help me ?


回答1:


The following works for me:

#include <string>
#include <iostream>
#include <Windows.h>

int main(void)
{
    // use utf8 literal
    std::string test = u8"1°à€3§4ç5@の,は,でした,象形字 ;"; 

    // set code page to utf8
    SetConsoleOutputCP(CP_UTF8);                        

    // Enable buffering to prevent VS from chopping up UTF-8 byte sequences
    setvbuf(stdout, nullptr, _IOFBF, 1000);

    // printing std::string to std::cout, not std::wstring to std::wcout
    std::cout << test << std::endl; 
}

But I had to change the font to SimSun-ExtB:

Then all the characters are shown:



来源:https://stackoverflow.com/questions/50179492/output-utf8-in-console-with-visual-studio-wide-stream

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