如何将qstring to char转换为char *或者相反

Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换
Qt再使用第三方开源库时,由于库的类型基本上都是标准的类型,字符串遇的多的就是Char*类型
在Qt下怎样将QString转char*呢,需要用到QByteArray类,QByteArray类的说明详见Qt帮助文档。
因为char*最后都有一个&/0&作为结束符,而采用QString::toLatin1()时会在字符串后面加上&/0&
方法如下:
QByteArray ba = str.toLatin1(); & &
ch=ba.data();
这样就完成了QString向char*的转化。经测试程序运行时不会出现bug
注意第三行,一定要加上,不可以str.toLatin1().data()这样一部完成,可能会出错。
补充:以上方法当QString里不含中文时,没有问题,但是QString内含有中文时,转换为char*就是乱码,采用如下方法解决:
添加GBK编码支持:
#include &QTextCodec&
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
然后改变上面的第三行为:QByteArray ba = str.toLoacl8Bit(); & & &toLoacl8Bit支持中文
先将QString转为标准库中的string类型,然后将string转为char*,如下:
std::string str = filename.toStdString();
const char* ch = str.c_str();
参考:/Romi/archive//2392478.html/wicbnu/archive//141956.aspx
-------------------------------------------------------------------------------------------
今天这个方法试了,还是没有成功,我用过toLatin1(),toAscii(),toLocal8Bit(),均没有成功换成先用std::string存储字符串,然后转成char*,成功了http://blog.csdn.net/gzshun/article/details/8526675
-------------------------------------------------------------------------------------------
在windows下的QT编程中的_TCHAR与QString之间的转换由于在windows下的QT编程中,如果涉及到使用微软的API,那么不可避免使用_TCHAR这些类型,因此在网上查了一下,其中一个老外的论坛有人给出了这个转换,因此在这里做一下笔记 : )#ifdef UNICODE#define QStringToTCHAR(x)
(wchar_t*) x.utf16()#define PQStringToTCHAR(x)
(wchar_t*) x-&utf16()#define TCHARToQString(x)
QString::fromUtf16((x))#define TCHARToQStringN(x,y)
QString::fromUtf16((x),(y))#else#define QStringToTCHAR(x)
x.local8Bit().constData()#define PQStringToTCHAR(x)
x-&local8Bit().constData()#define TCHARToQString(x)
QString::fromLocal8Bit((x))#define TCHARToQStringN(x,y)
QString::fromLocal8Bit((x),(y))#endif参考:http://blog.csdn.net/itjobtxq/article/details/8465194
阅读(...) 评论()的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益QT下QString转char*_百度知道
QT下QString转char*
RT这个要怎么转呢?网上看到的Qchar*QByteArray ba = str.toLatin1();ch=ba.data();按这个来转,结果error C2039: “toLatin1” : 不是“QString”的成员这个怎么回事呢?还有其他有效的方法吗?
提问者采纳
你用的什么版本的Qt啊,明明QString有这方法的。Q你源码就是这样写的吗?QString而不是Qstring
提问者评价
其他类似问题
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁7257人阅读
如何将QString转换为char *或者相反
How can I convert a QString to char* and vice versa ?(trolltech)
Inorder to convert a QString to a char*, then you first need to get alatin1 representation of the string by calling toLatin1() on it whichwill return a QByteArray. Then call data() on the QByteArray to get apointer 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)) ;
以上转自:/Alina-zl/archive//67323.html
<span style="color:#. 红色字体标出的存储那一步非常重要!!!!!!
<span style="color:#. 用toLatin1好像不支持中文,用toLocal8Bit可以支持。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:309708次
积分:4231
积分:4231
排名:第2427名
原创:101篇
转载:101篇
评论:97条
(1)(1)(11)(3)(5)(1)(1)(1)(4)(1)(1)(1)(2)(1)(1)(3)(5)(7)(8)(1)(1)(2)(2)(2)(1)(2)(5)(3)(5)(4)(4)(3)(5)(4)(1)(1)(8)(6)(5)(1)(5)(8)(6)(4)(6)(2)(2)(16)(2)(4)(2)(2)(3)(4)(4)(10)

我要回帖

更多关于 将char转换为string 的文章

 

随机推荐