Qt GUI API中使用Unicode类型的字符串类QString。使用Unicode的好处是可以支持更多的地区语言,ASCII是Uncode的子集。
QByteArray和QString的方法和属性相似,可以参考QString。另外有一些与QByteArray相关的非成员函数:
- //Related Non-Members
-
- //计算CRC16校验和
- quint16 qChecksum ( const char * data, uint len )
- //压缩data,并返回新的值,参数compressionLevel的值是0~9
- QByteArray qCompress ( const QByteArray & data, int compressionLevel = -1 )
- QByteArray qCompress ( const uchar * data, int nbytes, int compressionLevel = -1 )
- //解压缩
- QByteArray qUncompress ( const QByteArray & data )
- QByteArray qUncompress ( const uchar * data, int nbytes )
- //格式化字符串
- int qsnprintf ( char * str, size_t n, const char * fmt, ... )
- int qvsnprintf ( char * str, size_t n, const char * fmt, va_list ap )
- //比较字符串
- int qstrcmp ( const char * str1, const char * str2 )
- //复制字符串
- char * qstrcpy ( char * dst, const char * src )
- char * qstrncpy ( char * dst, const char * src, uint len )
- //分配空间复制字符串返回
- char * qstrdup ( const char * src )
- //忽略大小写的字符串比较
- int qstricmp ( const char * str1, const char * str2 )
- int qstrnicmp ( const char * str1, const char * str2, uint len )
- //返回字符串的长度
- uint qstrlen ( const char * str )
- uint qstrnlen ( const char * str, uint maxlen )
- //固定长度内的字符串比较
- int qstrncmp ( const char * str1, const char * str2, uint len )
- //相关QByteArray和const char *的运算符
- bool operator!= ( const QByteArray & a1, const QByteArray & a2 )
- bool operator!= ( const QByteArray & a1, const char * a2 )
- bool operator!= ( const char * a1, const QByteArray & a2 )
- const QByteArray operator+ ( const QByteArray & a1, const QByteArray & a2 )
- const QByteArray operator+ ( const QByteArray & a1, const char * a2 )
- const QByteArray operator+ ( const QByteArray & a1, char a2 )
- const QByteArray operator+ ( const char * a1, const QByteArray & a2 )
- const QByteArray operator+ ( char a1, const QByteArray & a2 )
- bool operator< ( const QByteArray & a1, const QByteArray & a2 )
- bool operator< ( const QByteArray & a1, const char * a2 )
- bool operator< ( const char * a1, const QByteArray & a2 )
- QDataStream & operator<< ( QDataStream & out, const QByteArray & ba )
- bool operator<= ( const QByteArray & a1, const QByteArray & a2 )
- bool operator<= ( const QByteArray & a1, const char * a2 )
- bool operator<= ( const char * a1, const QByteArray & a2 )
- bool operator== ( const QByteArray & a1, const QByteArray & a2 )
- bool operator== ( const QByteArray & a1, const char * a2 )
- bool operator== ( const char * a1, const QByteArray & a2 )
- bool operator> ( const QByteArray & a1, const QByteArray & a2 )
- bool operator> ( const QByteArray & a1, const char * a2 )
- bool operator> ( const char * a1, const QByteArray & a2 )
- bool operator>= ( const QByteArray & a1, const QByteArray & a2 )
- bool operator>= ( const QByteArray & a1, const char * a2 )
- bool operator>= ( const char * a1, const QByteArray & a2 )
- QDataStream & operator>> ( QDataStream & in, QByteArray & ba )
QByteArray与QString之间的转换:
示例:
QByteArray cstr("abcd");
QString s = cstr;
QByteArray cstr("abcd");
QString s;
s.prepend(cstr);
QString s("hello");
QByteArray cstr = s.toAscii();
QString s("hello");
QByteArray cstr = s.toLatin1();
QByteArray类同样不以'\0'为结尾:如
- QByteArray s;
- s.resize(5);
- s[0] = '1';
- s[1] = '2';
- s[2] = '3';
- s[3] = '\0';
- s[4] = 'a';
-
- cout << s << endl;
屏幕打印的是:“123 a”