如何将QString转换为char *或者相反
来源:互联网 发布:mac人民币符号怎么打 编辑:程序博客网 时间:2024/06/16 08:50
(注意:关注自己标红的,)
先看看官方是如何说的:
How can I convert a QString to char* and vice versa ?(trolltech)
Answer:
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)
{
}
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) )
QString str;
QCString cstr;
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);
方法二 -----------------------------------------
如果是中文系统
直接用
printf("%s", (const char*) str.local8Bit());
str是一个QString
方法三 -----------------------------------------
char str[64];
QTextCodec *textcod = QTextCodec::codecForName("GBK");
QString和Std::string
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后加上这句:
QUOTE:
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030"));
然后是从UTF8编码到GB编码的字符串转换方法:
QUOTE:
QString Utf8_To_GB(QString strText)
{
}
至于从GB到UTF8,那大家就经常用了:
QUOTE:
QString GB_To_Utf8(char *strText)
{
}
转自:http://blog.sina.com.cn/s/blog_3fd731da01009fgx.html
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 如何将QString转换为char *或者相反
- 怎样把QString转换为char *或者相反?
- 怎样把QString转换为char *或者相反?
- Qt中将QString转换为char *或者相反
- 怎样把QString转换为char *或者相反?
- printf函数的实现深入剖析
- 润乾报表在linux字体下的显示问题及几种解决办法
- 从北京大学老人42年冤屈看北大校长和拉黑中国史
- JS中URL编码与.net URL解码
- c 声明 && right-left rule
- 如何将QString转换为char *或者相反
- 几种常见的正则表达式验证(From ThinkPHP/Lib/Think/Core/Model.class.php)
- linux 内核配置机制(make menuconfig、Kconfig、makefile)讲解
- android java代码片断 摘录
- 全局变量的作用
- ApplicationContext(系统找不到指定的文件)异常(Z)
- Linux删除文件夹命令
- netty3.2.3源码分析--ServerBootstrap启动分析
- 拼分不拼命:高考冲刺应用推荐