码迷,mamicode.com
首页 > 其他好文 > 详细

QString unsigned char* 的转换

时间:2016-12-02 08:02:56      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:stat   signed   str   with   make   call   com   ascii   use   

QString -> unsigned char* :
QString str = "ABCD";
 int length = str.length();
unsigned char* sequence = NULL;
sequence =(unsigned char*)qstrdup(str.toAscii().constData());
delete[] sequence;
- sequence length = 5 --> [‘A‘] [‘B‘] [‘C‘] [‘D‘] [‘/0‘]
- sequence is now "independant" from str
- sequence has to be deleted with -> delete [] sequence
QString -> char:
const QByteArray ba = string.toAscii(); // make ba const, because modifying this array might otherwise invalidate the pointer
const char* sequence = ba.constData(); // now sequence will remain valid within the current scope.

The call to toAscii() creates a temporary QByteArray which goes out of scope when used like this:
char *sequence = string.toAscii().constData();
// sequence is now a dangling pointer!

 

http://wxpjiujiang.blog.163.com/blog/static/203994030201292661134137/

QString unsigned char* 的转换

标签:stat   signed   str   with   make   call   com   ascii   use   

原文地址:http://www.cnblogs.com/findumars/p/6124347.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!