wchar* 转char*与Qt下码制转换

栏目: 编程工具 · 发布时间: 8年前

内容简介:wchar* 转char*与Qt下码制转换

wchar* 转char*

Windows下wchar* 转char*有3种方法

1、wcstombs —Qt下测试不好使

WCHAR wtxt[512]=L"something中文";
char  txtRet[512]={0};
wcstombs(txtRet,wtxt, 2*wcslen(wtxt)+1 );
wcstombs_s(NULL,txtRet,512,wtxt,2*wcslen(wtxt)+1); //照样没效果,不知道为啥

2、WideCharToMultiByte —Qt下测试好使

WCHAR wtxt[512]=L"something中文";
char  txtRet[512]={0};
WideCharToMultiByte(CP_ACP,0,wtxt,wcslen(wtxt),txtRet,512,NULL,NULL);

3、Qt下用QString中转 —好使

WCHAR wtxt[512]=L"something中文";
std::cout<<QString::fromStdWString(wtxt).toLocal8Bit().data()<<std::endl;

补充

Qt下宽窄字符转换与码制转换小技巧。

utf8 转gbk

QString::fromStdString(std::string("")).tolocal8bit().data()

Windows系统下系统默认的是gbk,tolocal8Bit就转成gbk了。

而Qt编辑器默认的编码是utf8。以下语句qt会默认是从utf8编码取的字符串。而如果你的源字符串std::string是从系统api获取的,很可能就是gbk。此时就要用另一种方式加载。

QString(std::string("中文").c_str())

指定字符串的源编码格式代码如下:

QString::fromLocal8Bit(std::string("中文").c_str())

本文永久更新链接地址 http://www.linuxidc.com/Linux/2017-03/142021.htm


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Photoshop Anthology

The Photoshop Anthology

Corrie Haffly / SitePoint Pty. Ltd. / 2006 / USD 39.95

The Photoshop Anthology is full-color, question-and-answer book for Web Designers who want to use Photoshop to build Websites and create better looking web graphics more effectively. The book covers: ......一起来看看 《The Photoshop Anthology》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码