如何从c+;打印UTF-8+;Windows上的控制台应用程序 < >在Visual Studio 2008编译的C++ Windows控制台应用程序上,在Windows(XP,Vista或7)上编译。是否可以打印到控制台并使用cout或wcout正确显示UTF-8编码的日语?
在控制台中,输入如何从c+;打印UTF-8+;Windows上的控制台应用程序 < >在Visual Studio 2008编译的C++ Windows控制台应用程序上,在Windows(XP,Vista或7)上编译。是否可以打印到控制台并使用cout或wcout正确显示UTF-8编码的日语?,c++,utf-8,console-application,C++,Utf 8,Console Application,在控制台中,输入chcp 65001将代码页更改为UTF-8的代码页。Windows控制台默认使用显示输出 要将代码页更改为Unicode,请在控制台中输入chcp 65001,或尝试使用编程方式更改代码页 请注意,您可能必须将控制台的字体更改为unicode范围内有字形的字体。我从未尝试过将控制台代码页设置为UTF8(不确定为什么它不起作用…控制台可以很好地处理其他多字节代码页),但是有两个函数需要查找:SetConsoleCP和SetConsoleOutputCP 您可能还需要确保使用能够显
chcp 65001
将代码页更改为UTF-8的代码页。Windows控制台默认使用显示输出
要将代码页更改为Unicode,请在控制台中输入chcp 65001,或尝试使用编程方式更改代码页
请注意,您可能必须将控制台的字体更改为unicode范围内有字形的字体。我从未尝试过将控制台代码页设置为UTF8(不确定为什么它不起作用…控制台可以很好地处理其他多字节代码页),但是有两个函数需要查找:SetConsoleCP和SetConsoleOutputCP 您可能还需要确保使用能够显示字符的控制台字体。有这个功能,但它只在Vista和更高版本上可用
希望对您有所帮助。了解如何通过控制台正确输出UTF-16。您可以将UTF-8转换为UTF-16并输出 应用程序启动控制台设置为默认OEM437 CP。 我试图将Unicode文本输出到标准输出,控制台切换到UTF8翻译设置模式(_fileno(标准输出),_O_U8TEXT);即使是Lucida TT字体,在屏幕上也没有运气。 若控制台被重定向到文件,则创建了正确的UTF8文件 最后我很幸运。我添加了一行“info.fontflamily=FF_DONTCARE;”,它现在正在工作。 希望这对你有帮助
void SetLucidaFont()
{
HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_FONT_INFOEX info;
memset(&info, 0, sizeof(CONSOLE_FONT_INFOEX));
info.cbSize = sizeof(CONSOLE_FONT_INFOEX); // prevents err=87 below
if (GetCurrentConsoleFontEx(StdOut, FALSE, &info))
{
info.FontFamily = FF_DONTCARE;
info.dwFontSize.X = 0; // leave X as zero
info.dwFontSize.Y = 14;
info.FontWeight = 400;
_tcscpy_s(info.FaceName, L"Lucida Console");
if (SetCurrentConsoleFontEx(StdOut, FALSE, &info))
{
}
}
}
这应该起作用:
#include <cstdio>
#include <windows.h>
#pragma execution_character_set( "utf-8" )
int main()
{
SetConsoleOutputCP( 65001 );
printf( "Testing unicode -- English -- Ελληνικά -- Español -- Русский. aäbcdefghijklmnoöpqrsßtuüvwxyz\n" );
}
#包括
#包括
#杂注执行字符集(“utf-8”)
int main()
{
设置控制台输出端口CP(65001);
printf(“测试unicode——英语——西班牙语——西班牙语——aäbcdefghijklmnoöpqrsßtuüvwxyz\n”);
}
不知道它是否会影响任何内容,但源文件保存为Unicode(带签名的UTF-8)-代码页65001位于文件->高级保存选项…
项目->属性->配置属性->常规->字符集设置为使用Unicode字符集
有人说您需要将console字体更改为Lucida console,但在我这方面,它与console和Lucida console一起显示。仅提供其他信息: “ANSI”指windows-125x,用于win32应用程序,“OEM”指控制台/MS-DOS应用程序使用的代码页。
可以使用函数GetOEMCP()和GetACP()检索当前活动代码页 为了将内容正确地输出到控制台,您应该:
(如有必要,使用SetConsoleOutputCP正确设置)
// Convert a UTF-16 string (16-bit) to an OEM string (8-bit)
#define UNICODEtoOEM(str) WCHARtoCHAR(str, CP_OEMCP)
// Convert an OEM string (8-bit) to a UTF-16 string (16-bit)
#define OEMtoUNICODE(str) CHARtoWCHAR(str, CP_OEMCP)
// Convert an ANSI string (8-bit) to a UTF-16 string (16-bit)
#define ANSItoUNICODE(str) CHARtoWCHAR(str, CP_ACP)
// Convert a UTF-16 string (16-bit) to an ANSI string (8-bit)
#define UNICODEtoANSI(str) WCHARtoCHAR(str, CP_ACP)
/* Convert a single/multi-byte string to a UTF-16 string (16-bit).
We take advantage of the MultiByteToWideChar function that allows to specify the charset of the input string.
*/
LPWSTR CHARtoWCHAR(LPSTR str, UINT codePage) {
size_t len = strlen(str) + 1;
int size_needed = MultiByteToWideChar(codePage, 0, str, len, NULL, 0);
LPWSTR wstr = (LPWSTR) LocalAlloc(LPTR, sizeof(WCHAR) * size_needed);
MultiByteToWideChar(codePage, 0, str, len, wstr, size_needed);
return wstr;
}
/* Convert a UTF-16 string (16-bit) to a single/multi-byte string.
We take advantage of the WideCharToMultiByte function that allows to specify the charset of the output string.
*/
LPSTR WCHARtoCHAR(LPWSTR wstr, UINT codePage) {
size_t len = wcslen(wstr) + 1;
int size_needed = WideCharToMultiByte(codePage, 0, wstr, len, NULL, 0, NULL, NULL);
LPSTR str = (LPSTR) LocalAlloc(LPTR, sizeof(CHAR) * size_needed );
WideCharToMultiByte(codePage, 0, wstr, len, str, size_needed, NULL, NULL);
return str;
}
对于需要从文件读取UTF-8并打印到控制台的任何人,可以尝试
wifstream
,即使在visual studio中,调试器也可以正确显示UTF-8单词(我正在处理繁体中文),来自:
#包括
#包括
#包括
std::wstring读取文件(常量字符*文件名)
{
std::wifstream wif(文件名);
imbue(std::locale(std::locale::empty(),新std::codevt_utf8));
std::wstringstream wss;
wss在cplusplus.com上有一个有趣的帖子:命令提示符(控制台)中的UTF-8新来者:关于使用unicode,并检查控制台字体的更改。一个观察:Michael Kaplan不是MVP。我不知道他是否是。他已经是Microsoft员工很多年了(Microsoft员工没有资格参加MVP计划)。此链接指向未找到的资源。截至2016-08-09,该链接重定向到,但存在材料(可能是最初在siao2.com上的内容).Permalink:请注意,UTF-8作为代码页存在许多错误。大多数错误都是WONTFIX。默认cmd控制台位于代码页850中。此解决方案似乎无法在默认设置上工作。您是否更改了其他设置?@Sandburg我不记得更改了任何其他内容,但建议可能与我的Windows为Rus进行本地化有关西安。
#include <sstream>
#include <fstream>
#include <codecvt>
std::wstring readFile(const char* filename)
{
std::wifstream wif(filename);
wif.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
std::wstringstream wss;
wss << wif.rdbuf();
return wss.str();
}
// usage
std::wstring wstr2;
wstr2 = readFile("C:\\yourUtf8File.txt");
wcout << wstr2;