#include <iostream>
using namespace std;
#ifdef _WIN32
#include <windows.h>
#endif
int main()
{
#ifdef _WIN32
//控制台显示乱码纠正
SetConsoleOutputCP (65001);
CONSOLE_FONT_INFOEX info = { 0 }; // 以下设置字体来支持中文显示。
info.cbSize = sizeof(info);
info.dwFontSize.Y = 16;
info.FontWeight = FW_NORMAL;
wcscpy(info.FaceName, L"NSimSun");//指定新宋体字体
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &info);
#endif
cout << "中文乱码解决!" << endl;
return 0;
}
有时候打印路径的时候会出现方框之类的错误显示。以下为终极解决方案
#include <iostream>
#include <windows.h>
void utf8ToGbk(char *utf8String, char *gbkString);
using namespace std;
int main(int argc, char **argv)
{
//程序开始
char text[MAX_PATH]="中文测试";
char retText[MAX_PATH]={"\0"};
utf8ToGbk(text,retText);
cout << retText << endl;
return 0;
}
void utf8ToGbk(char *utf8String, char *gbkString)
{
wchar_t *unicodeStr = NULL;
int nRetLen = 0;
nRetLen = MultiByteToWideChar(CP_UTF8, 0, utf8String, -1, NULL, 0);
//求需求的宽字符数大小
unicodeStr = (wchar_t *)malloc(nRetLen * sizeof(wchar_t));
nRetLen = MultiByteToWideChar(CP_UTF8, 0, utf8String, -1, unicodeStr, nRetLen);
//将utf-8编码转换成unicode编码
nRetLen = WideCharToMultiByte(CP_ACP, 0, unicodeStr, -1, NULL, 0, NULL, 0);
//求转换所需字节数
nRetLen = WideCharToMultiByte(CP_ACP, 0, unicodeStr, -1, gbkString, nRetLen, NULL, 0);
//unicode编码转换成gbk编码
free(unicodeStr);
}
标签:info,char,中文,Vscode,C++,乱码,int,include
From: https://www.cnblogs.com/xiaohoulaoyue/p/16914801.html