小标
2018-08-28
来源 :
阅读 2405
评论 0
摘要:本文主要向大家介绍了VC编程之Windows下VC++显示UTF-8编码中文,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。
本文主要向大家介绍了VC编程之Windows下VC++显示UTF-8编码中文,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。
笔者在Windows下使用C++编写程序接收UTF8字符串会发生中文无法正常在console上显示的问题,故特来解决UTF8字符串如何在VC++上正常显示。
1、问题重现,UTF-8编码下的字符串“中文”在Windows下的显示如下图,其中上行为UTF8字符串,下行为GBK字符串:
2、方法:将UTF-8格式的代码变为GBK编码(需要Windows.h头文件)
1 string UTF8ToGBK(string &strUtf8)
2 {
3 string strOutGBK = "";
4 int len = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, NULL, 0);
5 WCHAR *wszGBK = new WCHAR[len + 1];
6 memset(wszGBK, 0, len * 2 + 2);
7 MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, wszGBK, len);
8 len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
9 char *pszGBK = new char[len + 1];
10 memset(pszGBK, 0, len + 1);
11 WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, pszGBK, len, NULL, NULL);
12 strOutGBK = pszGBK;
13 delete[]pszGBK;
14 delete[]wszGBK;
15 return strOutGBK;
16 }
调用例子如下:
1 string strGBK = UTF8ToGBK(strUTF8);//strUTF8为UTF8字符串
2 cout << "字符串(UTF-8):"<< strUTF8 << endl;
3 printf( "字符串( GBK ):%s\n", strGBK.c_str());
结果如下:
附上GBK格式的代码变为UTF-8编码函数:
1 string GBKToUTF8(const std::string& strGBK)
2 {
3 string strOutUTF8 = "";
4 WCHAR * str1;
5 int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
6 str1 = new WCHAR[n];
7 MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
8 n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
9 char * str2 = new char[n];
10 WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
11 strOutUTF8 = str2;
12 delete[]str1;
13 str1 = NULL;
14 delete[]str2;
15 str2 = NULL;
16 return strOutUTF8;
17 }
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言VC/MFC频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号