首页 > 其他分享 >Qt5.12实战之字节数组QByteArray使用

Qt5.12实战之字节数组QByteArray使用

时间:2023-03-20 10:32:02浏览次数:40  
标签:QByteArray Qt5.12 int unsigned test ba qDebug type 字节



Qt5.12实战之字节数组QByteArray使用_#include



Qt5.12实战之字节数组QByteArray使用_开发语言_02



Qt5.12实战之字节数组QByteArray使用_Powered by 金山文档_03


示例源码:

#include <QCoreApplication>
#include <QDebug>
#include <QTextStream>
static QTextStream cout (stdout,QIODevice::WriteOnly);
#include <iostream>
#include <QtGlobal>
#include <QByteArray>

void test()
{
qDebug() << "HelloWorld-qdebug";
cout << "Helloword-QTextStream" <<endl;
std::cout<<"Helloword-std::cout"<<std::endl;
}

void test_types()
{
qint8 signed_char_type;//signed char 有符号8bit
qint16 signed_short_type;//signed short 有符号16bit
qint32 signed_int_type;//signed int 有符号32bit
qint64 signed_long_long_int_type;//signed long long int 有符号64bit

qDebug() <<"sizeof(qint8)=" <<sizeof (signed_char_type)
<<"sizeof(qint16)=" <<sizeof (signed_short_type)
<<"sizeof(qint32)=" <<sizeof(signed_int_type)
<<"sizeof(qint64)=" <<sizeof (signed_long_long_int_type)<<endl;;

qptrdiff qint32_or_qint64_type;//32位系统 qint32 64位系统 qint64
qreal double_or_float_type;//默认是double,如果加上-qreal float选项,那么是float
qintptr same_as_qptrdiff;//32位系统 qint32 64位系统 qint64
quintptr quint32_or_quint64_type;//32位系统 quint32 64位系统 quint64
qDebug() <<"sizeof(qptrdiff)=" <<sizeof (qint32_or_qint64_type)
<<"sizeof(qreal)=" <<sizeof (double_or_float_type)
<<"sizeof(qintptr)=" <<sizeof(same_as_qptrdiff)
<<"sizeof(quintptr)=" <<sizeof (quint32_or_quint64_type)<<endl;

quint8 unsigned_char_type;//unsigned char 无符号8bit
quint16 unsigned_short_type;//unsigned short 无符号16bit
quint32 unsigned_int_type;//unsigned int 无符号32bit
quint64 unsigned_long_long_int_type;//unsigned long long int 无符号64bit
qDebug() <<"sizeof(quint8)=" <<sizeof (unsigned_char_type)
<<"sizeof(quint16)=" <<sizeof (unsigned_short_type)
<<"sizeof(quint32)=" <<sizeof(unsigned_int_type)
<<"sizeof(quint64)=" <<sizeof (unsigned_long_long_int_type)<<endl;

qlonglong same_as_qint64_type;//相当于qint64
qulonglong same_as_quint64_type;//相当于quint64
qDebug() <<"sizeof(qlonglong)=" <<sizeof (same_as_qint64_type)
<<"sizeof(qulonglong)=" <<sizeof (same_as_quint64_type)<<endl;

uchar unsigned_char_type_same_quint8;//unsigned char 无符号8bit
ushort unsigned_short_type_same_quint16;//unsigned short 无符号16bit
uint unsigned_int_type_same_quint32;//unsigned int 无符号32bit
ulong unsigned_long_type;//unsigned long 无符号32bit
qDebug() <<"sizeof(uchar)=" <<sizeof (unsigned_char_type_same_quint8)
<<"sizeof(ushort)=" <<sizeof (unsigned_short_type_same_quint16)
<<"sizeof(uint)=" <<sizeof(unsigned_int_type_same_quint32)
<<"sizeof(ulong)=" <<sizeof (unsigned_long_type)<<endl;

}

void test_qbytearray()
{
QByteArray bArray("Hello QByteArray"); // '\0'
int len = bArray.length();
char cLast = bArray[17];
qDebug()<<"string len:"
<<len
<<"last char:"
<<cLast
<<endl;
}

void test_qbytearray_resize()
{
QByteArray ba;
ba.resize(3);
ba[0]=0x3c;
ba[1]=0xb8;
ba[2]=0x64;
qDebug()<<endl<<"ba length:"<<ba.length()<<endl;
}

void test_qbytearray_fill()
{
QByteArray ba("hello");
qDebug()<<"ba before fill:"<<ba<<endl;
ba.fill('K');
qDebug()<<"ba after fill:"<<ba<<endl;
ba.fill('K',2);
qDebug()<<"ba after fill two char:"<<ba<<endl;
ba.fill('Q',10);
qDebug()<<"ba after fill ten char Q:"<<ba
<<"qbytearray length:"<<ba.length()
<<"sizeof(ba)="<<sizeof (ba)<<endl //ba is a point of QByteArray
<<"sizeof(QByteArray[])="<<sizeof ( (char)ba[1])<<endl;
ba.data()[7]='A';
qDebug()<<ba.at(4)<<ba.data()[5]<<ba.constData()[7]<<endl;
}

void test_qbytearray_right_left_mid()
{
QByteArray ba("HelloWorld2023");
qDebug()<<ba.left(5);
qDebug()<<ba.right(4);
qDebug()<<ba.mid(5,5);
}

void test_qbytearray_size()
{
QByteArray ba("HelloWorld");
qDebug()<<ba.length();
qDebug()<<ba.size();
qDebug()<<ba.count();
QByteArray ba1("He\0llo");
qDebug()<<ba1.length();
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
test();
test_types();
test_qbytearray();
test_qbytearray_resize();
test_qbytearray_fill();
test_qbytearray_right_left_mid();
test_qbytearray_size();
return a.exec();
}

标签:QByteArray,Qt5.12,int,unsigned,test,ba,qDebug,type,字节
From: https://blog.51cto.com/remotedev/6132095

相关文章