在Windows控制台应用程序中输出Unicode字符串 [英] Output unicode strings in Windows console app

查看:103
本文介绍了在Windows控制台应用程序中输出Unicode字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我发现这个: http://stackoverflow.com/questions/1922294/using-unicode-font-in-c-console-app\">在c ++控制台应用程序中使用unicode字体,此代码段可以工作。

  SetConsoleOutputCP(CP_UTF8); 
wchar_t s [] = LèéøÞǽлљΣæča;
int bufferSize = WideCharToMultiByte(CP_UTF8,0,s,-1,NULL,0,NULL,NULL);
char * m = new char [bufferSize];
WideCharToMultiByte(CP_UTF8,0,s,-1,m,bufferSize,NULL,NULL);
wprintf(L%S,m);

然而,我没有找到任何方法正确输出unicode与iostreams。有任何建议吗?



这不起作用:

  SetConsoleOutputCP(CP_UTF8 ); 
utf8_locale = locale(old_locale,new boost :: program_options :: detail :: utf8_codecvt_facet());
wcout.imbue(utf8_locale);
wcout<< L¡ << endl;

EDIT
我找不到任何其他解决方案,将此代码段包裹在流中。
希望,有人有更好的想法。

  // Windows控制台的Unicode输出
ostream& operator-(ostream& stream,const wchar_t * s)
{
int bufSize = WideCharToMultiByte(CP_UTF8,0,s,-1,NULL,0,NULL,NULL);
char * buf = new char [bufSize];
WideCharToMultiByte(CP_UTF8,0,s,-1,buf,bufSize,NULL,NULL);
wprintf(L%S,buf);
delete [] buf;
return stream;
}

ostream& operator-(ostream& stream,const wstring& s)
{
stream - s.c_str
return stream;
}


解决方案

使用Visual Studio 2010.通过此 MSDN文章 MSDN博客文章。窍门是对 _setmode(...,_O_U16TEXT)的模糊调用。



解决方案: / strong>

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

int wmain(int argc,wchar_t * argv [])
{
_setmode(_fileno(stdout),_O_U16TEXT);
std :: wcout<< L测试unicode - 英语 - Ελληνικά - Español。 << std :: endl;
}

屏幕截图: b


Hi I was trying to output unicode string to a console with iostreams and failed.

I found this: Using unicode font in c++ console app and this snippet works.

SetConsoleOutputCP(CP_UTF8);
wchar_t s[] = L"èéøÞǽлљΣæča";
int bufferSize = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL);
char* m = new char[bufferSize]; 
WideCharToMultiByte(CP_UTF8, 0, s, -1, m, bufferSize, NULL, NULL);
wprintf(L"%S", m);

However, I did not find any way to output unicode correctly with iostreams. Any suggestions?

This does not work:

SetConsoleOutputCP(CP_UTF8);
utf8_locale = locale(old_locale,new boost::program_options::detail::utf8_codecvt_facet());
wcout.imbue(utf8_locale);
wcout << L"¡Hola!" << endl;

EDIT I could not find any other solution than to wrap this snippet around in a stream. Hope, somebody has better ideas.

//Unicode output for a Windows console 
ostream &operator-(ostream &stream, const wchar_t *s) 
{ 
    int bufSize = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL);
    char *buf = new char[bufSize];
    WideCharToMultiByte(CP_UTF8, 0, s, -1, buf, bufSize, NULL, NULL);
    wprintf(L"%S", buf);
    delete[] buf; 
    return stream; 
} 

ostream &operator-(ostream &stream, const wstring &s) 
{ 
    stream - s.c_str();
    return stream; 
} 

解决方案

I have verified a solution here using Visual Studio 2010. Via this MSDN article and MSDN blog post. The trick is an obscure call to _setmode(..., _O_U16TEXT).

Solution:

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

int wmain(int argc, wchar_t* argv[])
{
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << L"Testing unicode -- English -- Ελληνικά -- Español." << std::endl;
}

Screenshot:

这篇关于在Windows控制台应用程序中输出Unicode字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆