&#8203.11596;qDebug()<<QString::fromUtf8(strr.c_str());无法输出正确文本c++

3098人阅读
1.将QString转换为std::string,可以通过QString的成员函数toStdString()
QString Qstr=&123&;std::string str=Qstr.toStdString();
2.将QString转换为char *或者相反
直接转换不行,因为QString没有提供直接的成员函数,但是可以通过QByteArray中转一下,例如:
int main(int argc, char **argv)
&&&& QApplication app(argc, argv);
&&&& QString str1 = &Test&;
&&&& QByteArray ba = str1.toLatin1();
&&&& const char *c_str2 = ba.data();
&&&& printf(&str2: %s&, c_str2);
&&&& return app.exec();&&&&
还有其他多种方法:
方法一 -----------------------------------------
#define G2U(s) ( QTextCodec::codecForName(&GBK&)-&toUnicode(s) )
#define U2G(s) ( QTextCodec::codecForName(&GBK&)-&fromUnicode(s) )
str = G2U(&中文输入&);
cstr = U2G(str);
QCString有这样一个重载运算符
operator const char * () const
printf(&%s\n&, (const char*) cstr);
或是copy出来
char buf[1024];
strcpy(buf, (const char*) cstr);
方法二 -----------------------------------------
如果是中文系统 直接用&& (const char*) str.local8Bit()
printf(&%s&, (const char*) str.local8Bit());
str是一个QString
方法三 -----------------------------------------
char str[64];
QTextCodec *textcod = QTextCodec::codecForName(&GBK&);
&&&&&&& QCString string1 = textcod -&fromUnicode(listbox1-&currentText());
&&&&&&& strcpy(str,string1);
QString和Std::string
从char*到 QString可以从fromLocal8Bit()转化std::string有c_str()的函数使再转化为char*QString有toAscii()记不清了
你可以看看.
又是我的粗心酿成大错,我重新查看了一下Qt文档,原来Qt可以直接从std::wstring产生一个QString,用QString::fromStdWString(const std::wstring &)这个静态成员函数即可。我试了试用std::string的c_str()返回的char *构造的QString不能再保存原先的中文信息,而用std::wstring构造的QString则可以用qDebug()输出原先的中文信息GB编码与UTF8编码的转换在主函数app后加上这句:
QTextCodec::setCodecForLocale(QTextCodec::codecForName(&GB18030&));
然后是从UTF8编码到GB编码的字符串转换方法:
QString Utf8_To_GB(QString strText)
&&& return QString::fromUtf8(strText.toLocal8Bit().data());
至于从GB到UTF8,那大家就经常用了:
QString GB_To_Utf8(char *strText)
&&& return QString::fromLocal8Bit(strText);
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:56146次
排名:千里之外
原创:30篇
转载:74篇
(2)(1)(1)(2)(2)(27)(17)(8)(13)(5)(7)(8)(13)1066人阅读
首先声明,此文摘自,再次予以感谢!
传给未分配内存的const char* (LPCTSTR)指针.
&& CString cstr(asdd);
&& const char* ch = (LPCTSTR)
&& ch指向的地址和cstr相同。但由于使用const保证ch不会修改,所以安全.2.传给未分配内存的指针.
&&& CString cstr = &ASDDSD&;
&&& char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
&&& cstr.ReleaseBuffer();
&&& //修改ch指向的值等于修改cstr里面的值.
&&& //PS:用完ch后,不用delete ch,因为这样会破坏cstr内部空间,容易造成程序崩溃.
3.第二种用法。把CString 值赋给已分配内存的char *。
&&& CString cstr1 = &ASDDSD&;
&&& int strLength = cstr1.GetLength() + 1;
&&& char *pValue = new char[strLength];
&&& strncpy(pValue, cstr1, strLength);
4.第三种用法.把CString 值赋给已分配内存char[]数组.
&&& CString cstr2 = &ASDDSD&;
&&& int strLength1 = cstr1.GetLength() + 1;
&&& char chArray[100];
&&& memset(chArray,0, sizeof(bool) * 100); //将数组的垃圾内容清空.
&&& strncpy(chArray, cstr1, strLength1);
如果上述都不行:
CString转换为char*
&&&&&&& CString origCString(&Hello, World!&);
&&&&&&& wchar_t* wCharString = origCString.GetBuffer(origCString.GetLength()+1);
&&&&&&& size_t origsize = wcslen(wCharString) + 1;
&&&&&&& size_t convertedChars = 0;
&&&&&&& char *CharS
&&&&&&& CharString=new char(origsize);
&&&&&&& wcstombs_s(&convertedChars, CharString, origsize, wCharString , _TRUNCATE);
&&&&&&& cout && CharString &&
成功输出字符串&Hello,World&
原来在VC++ 2005以前,应用程序默认都是关闭对Unicode的支持的,而在VC2005中,默认打开了对它的支持,CString对应的字符串应该是TCHAR,TCHAR的定义是这样的,
&&&&&&& #ifdef _UNICODE
&&&&&&& typedef wchar_t TCHAR&&& ;
&&&&&&& #else
&&&&&&& typedef char TCHAR;
&&&&&&& #endif
所以在工程中应该可以关闭对于Unicode的支持,从而可以直接转换。这个做法是右击工程名—〉Property—〉General中的character set中选择notset,这样,本文开头的那段代码就可以正确的执行了。
如何将QString转换为char *或者相反
How can I convert a QString to char* and vice versa ?(trolltech)
In order to convert a QString to a char*, then you first need to get a latin1 representation of the string by calling toLatin1() on it which will return a QByteArray. Then call data() on the QByteArray to get a pointer to the data stored in the byte array.
See the documentation:
See the following example for a demonstration:
int main(int argc, char **argv)
&QApplication app(argc, argv);
&QString str1 = &Test&;
&QByteArray ba = str1.toLatin1();
&const char *c_str2 = ba.data();
&printf(&str2: %s&, c_str2);
&return app.exec();&&
Note that it is necessary to store the bytearray before you call data() on it, a call like the following
const char *c_str2 = str2.toLatin1().data();
will make the application crash as the QByteArray has not been stored and hence no longer exists.
To convert a char* to a QString you can use the QString constructor that takes a QLatin1String, e.g:
QString string = QString(QLatin1String(c_str2)) ;
还有其他多种方法:
方法一 -----------------------------------------
#define G2U(s) ( QTextCodec::codecForName(&GBK&)-&toUnicode(s) )
#define U2G(s) ( QTextCodec::codecForName(&GBK&)-&fromUnicode(s) )
str = G2U(&中文输入&);
cstr = U2G(str);
QCString有这样一个重载运算符
operator const char * () const
printf(&%s\n&, (const char*) cstr);
或是copy出来
char buf[1024];
strcpy(buf, (const char*) cstr);
方法二 -----------------------------------------
如果是中文系统
直接用&& (const char*) str.local8Bit()
printf(&%s&, (const char*) str.local8Bit());
str是一个QString
方法三 -----------------------------------------
char str[64];
QTextCodec *textcod = QTextCodec::codecForName(&GBK&);
&&&&&&& QCString string1 = textcod -&fromUnicode(listbox1-&currentText());
&&&&&&& strcpy(str,string1);
QString和Std::string
&从char*到 QString可以从fromLocal8Bit()转化
std::string有c_str()的函数使再转化为char*
QString有toAscii()记不清了
你可以看看.
又是我的粗心酿成大错,我重新查看了一下Qt文档,原来Qt可以直接从std::wstring产生一个QString,用QString::fromStdWString(const std::wstring &)这个静态成员函数即可。我试了试用std::string的c_str()返回的char *构造的QString不能再保存原先的中文信息,而用std::wstring构造的QString则可以用qDebug()输出原先的中文信息
GB编码与UTF8编码的转换
在主函数app后加上这句:
QTextCodec::setCodecForLocale(QTextCodec::codecForName(&GB18030&));
然后是从UTF8编码到GB编码的字符串转换方法:
QString Utf8_To_GB(QString strText)
&&& return QString::fromUtf8(strText.toLocal8Bit().data());
至于从GB到UTF8,那大家就经常用了:
QString GB_To_Utf8(char *strText)
&&& return QString::fromLocal8Bit(strText);
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:71632次
排名:千里之外
原创:10篇
转载:18篇
评论:30条
(1)(1)(2)(2)(1)(2)(6)(12)(1)qt 编码问题_百度知道
qt 编码问题
windows默认的编码是GBK,linux默认的编码是UTF8我在windows环境下使用Qt编程的时候,只有使用GBK才不会乱码,请问我怎么设置才能在windows下使用utf运行处汉字。我用GBK的代码如下:QTextCodec::setCodecForLocale(QTextCodec::codecForName(&GBK&));QTextCodec::setCodecForCStrings(QTextCodec::codecForName(&GBK&));QTextCodec::setCodecForTr(QTextCodec::codecForName(&GBK&));qDebug()&&&不怕神一样的对手,就怕猪一样的队友&;我想吧GBK换成UTF-8,换了就会乱码设置Qt的编码是在这吗?我说的是Qt CreatorTools-&Options-&Environment-&General-&Default file encoding. 是这吗?如果不是,再哪设置,我设置这个没发现它有什么作用
没有人知道吗?
提问者采纳
windows下默认是GBK的编码格式,如果想使用UTF8就要先修改Qt Creator的编码格式,方法如下Tools-&Options-&Environment-&General-&Default file encoding修改好UTF8格式之后,再创建工程,就可以显示UTF8格式的汉字了代码如下:QTextCodec::setCodecForLocale(QTextCodec::codecForName(&UTF8&));QTextCodec::setCodecForCStrings(QTextCodec::codecForName(&UTF8&));QTextCodec::setCodecForTr(QTextCodec::codecForName(&UTF8&));qDebug()&&&不怕神一样的对手,就怕猪一样的队友&;或者使用这样输出QTextCodec* codec = QTextCodec::codecForName(&UTF8&)QString str = codec-&toUnicode(&不怕神一样的对手,就怕猪一样的队友&);qDebug()&&
提问者评价
其他类似问题
为您推荐:
其他1条回答
你在windows下文件格式保存为utf-8,这样就没有问题了。因为在默认情况下,linux下的utf8文本是没有包含utf8文件头信息的,所以会导致windows下处理有不兼容
您可能关注的推广回答者:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁用心创造滤镜
扫码下载App
汇聚2000万达人的兴趣社区下载即送20张免费照片冲印
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
1. QString str = QString::fromLocal8Bit("中文"); // vs2008 vs20052. QString str = QString::fromLocal8Bit("中文"); // &gcc vs2003, 如源码是 GBK 编码(记事本中的 ANSI 编码)3.QString str = QString::fromUtf8("中文"); & & & & &// &gcc vs2003, 如源码是 UTF-8 编码在QT程序中, 如果直接用QString保存中文字符串,则会出现乱码,比如下面的程序输出不是 "中文" 两个字, 而是乱码:#include &QCoreApplication&#include &QString&#include &QDebug&int main(int argc, char *argv[]){& & QCoreApplication a(argc, argv);& & QString str("中文");& & qDebug() &&& & return a.exec();}输出:&===============================================================来看QString文档:QString str("中文"); 这句调用的QString构造数是下面这个, 只有这个和参数是匹配的:QString::QString ( const char *&str&)看介绍:Constructs a string initialized with the 8-bit string&str. The given const char pointer is converted to Unicode using the&fromAscii() function.使用一个8位的字符串来初使化一个QString.会调用fromAscii函数把这个字符串转化成Unicode字符串.即然fromAscii会把这个字符串转化成Unicode字符串,那为什么会出现乱码呢?来看看fromAscii的源码(源码可以在Qt安装目录下面的src目录下找到):QString QString::fromAscii(const char *str, int size){& & return QString(fromAscii_helper(str, size), 0);}fromAscii会调用下面这个函数:QString::Data *QString::fromAscii_helper(const char *str, int size){#ifndef QT_NO_TEXTCODEC& & if (codecForCStrings) {& & & & Data *d;& & & & if (!str) {& & & & & & d = &shared_& & & & & & d-&ref.ref();& & & & } else if (size == 0 || (!*str && size & 0)) {& & & & & & d = &shared_& & & & & & d-&ref.ref();& & & & } else {& & & & & & if (size & 0)& & & & & & & & size = qstrlen(str);& & & & & & QString s = codecForCStrings-&toUnicode(str, size); // 关键是这句& & & & & & d = s.d;& & & & & & d-&ref.ref();& & & & }& & & && & }#endif& & return fromLatin1_helper(str, size);}从红色的那句可以知道, fromAscii最终是通过&codecForCStrings 把字符串转换成QString的在qstring.h头文件中可以看到如下的定义:#ifndef QT_NO_TEXTCODEC& & static QTextCodec *codecForCS#endif现在知道字符串是通过一个QTextCodec对象来转换成一个字符串的.看toUnicode的定义:QString&QTextCodec::toUnicode ( const&Char *&a&,&int&size,&ConverterState&*&state&= 0 ) constConverts&a&from the encoding of this codec to Unicode, and returns the result in a&QString.把字符串a从codecForCStrings所表示的编码转换到Unicode编码.前面写的 str("中文"); 出现的乱码, 很有可能是因为codecForCStrings所表示的编码不对.在QTextCodeC中有这样一个函数:voidsetCodecForCStrings&( QTextCodec *&codec&)这是一个静态函数看它的实现代码, 在文件qtextcodec.h中:inline void QTextCodec::setCodecForCStrings(QTextCodec *c) { QString::codecForCStrings = }只有一句话, 就是设置codecForCStrings的值, 这就是用于把 char * 转换成Unicode的对象.我们来试试重新设置一下&codecForCStrings 对象,修改一下它所表示的编码, 下面是修改后的代码:#include &QCoreApplication&#include &QString&#include &QDebug&#include &QTextCodec&int main(int argc, char *argv[]){& & QCoreApplication a(argc, argv);& &&QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK")); // 关键是这句& & QString str("乱码");& & qDebug() &&& & return a.exec();}编译运行看结果:正如期待的一样, 可以正确的显示中文.那么为什么加上那句就可以正确显示中文了呢?&QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK")); // 关键是这句加这句是认为字符串 "乱码" 是GBK编码的.结果非常的 "巧合" 它正好是GBK编码的.所以结果就正确了.为什么设为GBK编码就可以了呢??&因为我用的是 Visual Studio 2008 编译的, vs2008 编译器编译时会把 字符串 用locale字符编码字符串我的系统编码是GBK, 所以就正确了.至于其它的编译器, 请参考链接中的文章...大牛写的.vs2008, vs2005.编译时不管源码是什么编码都会把源码中的字符串转换成 locale 编码(中文系统就是GBK),& & & & & & & & & & & 有两种方法处理中文:& & & & & & & & & & & 1.&QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK")); // 关键是这句& & & & & & & & & 2.QString str = QString::fromLocal8bit("中文");vs2003, & &1. 如果源码是 ANSI(中文系统中的GBK编码) 编码的, 则在程序中可以这样处理中文:& & & & & & & & & & & & & QString str = QString::fromLocal8bit("中文");& & & & & & & & 2. 如果源码是 UTF-8 编码的则在程序中可以这样处理中文:& & & & & & & & & & & & & QString str = QString::fromUtf8("中文");gcc 和 vs2003一样的处理, 不过gcc有编译选项可以设置, 请参数后面的链接.
阅读(5462)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'QString中文乱码',
blogAbstract:'处理方法:1. QString str = QString::fromLocal8Bit(\"中文\"); // vs2008 vs20052. QString str = QString::fromLocal8Bit(\"中文\"); // &gcc vs2003, 如源码是 GBK 编码(记事本中的 ANSI 编码)3.QString str = QString::fromUtf8(\"中文\");',
blogTag:'qstring,中文乱码,乱码,qstextstream',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:6,
publishTime:4,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}

我要回帖

更多关于 dm8203 的文章

 

随机推荐