效果:
视频:
示例源码:
#include <QCoreApplication>标签:实战,QByteArray,Qt5.12,unsigned,test,qDebug,type,ba From: https://blog.51cto.com/remotedev/6132572
#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();
}
void test_datatype_convert()
{
QByteArray ba("HelloWorld");
QByteArray ba_hex = ba.toHex();
qDebug() << "HelloWorld to Hex: "<<ba_hex<<endl; //48656c6c6f576f726c64
QByteArray ba_fromhex = QByteArray::fromHex("48656c6c6f576f726c64");
qDebug() << "QByteArray from Hex: "<<ba_fromhex<<endl;
int digit = 255;
uint u_digit = 255u;
qlonglong qll_digit = 255ll;
qulonglong qull_digit = 255ull;
qDebug()<<"number base 10 on int :"<< QByteArray::number(digit);
qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit);
qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit);
qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit);
qDebug()<<"number base 10 on int :"<< QByteArray::number(78);
qDebug()<<"number base 10 on uint :"<<QByteArray::number(78u);
qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(78ll);
qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(78ull);
qDebug()<<"number base 10 on int :"<< QByteArray::number(digit,16);
qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit,16);
qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit,16);
qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit,16);
qDebug()<<"number base 10 on int :"<< QByteArray::number(digit,8);
qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit,8);
qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit,8);
qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit,8);
qDebug()<<"number base 10 on int :"<< QByteArray::number(digit,2);
qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit,2);
qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit,2);
qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit,2);
QByteArray num;
qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit);
qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit);
qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit);
qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit);
qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit,16);
qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit,16);
qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit,16);
qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit,16);
qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit,8);
qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit,8);
qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit,8);
qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit,8);
qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit,2);
qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit,2);
qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit,2);
qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit,2);
double dbNum=12345.66;
qDebug()<<"number base 10 on double of e format(prec=6) :"<< QByteArray::number(dbNum,'e');
qDebug()<<"number base 10 on double of e format(prec=3) :"<< QByteArray::number(dbNum,'e',3);
qDebug()<<"number base 10 on double of E format(prec=6) :"<< QByteArray::number(dbNum,'E');
qDebug()<<"number base 10 on double of e format(prec=3) :"<< QByteArray::number(dbNum,'E',3);
qDebug()<<"number base 10 on double of f format(prec=6) :"<< QByteArray::number(dbNum,'f');
qDebug()<<"number base 10 on double of f format(prec=3) :"<< QByteArray::number(dbNum,'f',3);
qDebug()<<"number base 10 on double of f format(prec=3) :"<< QByteArray::number(dbNum,'f',1);
qDebug()<<"number base 10 on double of g format(prec=6) :"<< QByteArray::number(dbNum,'g');
qDebug()<<"number base 10 on double of g format(prec=3) :"<< QByteArray::number(dbNum,'g',3);
qDebug()<<"number base 10 on double of G format(prec=6) :"<< QByteArray::number(dbNum,'G');
qDebug()<<"number base 10 on double of G format(prec=3) :"<< QByteArray::number(dbNum,'G',3);
QByteArray abc="HelloWorld";
qDebug()<<"HelloWorld ->toLower() : "<<abc.toLower();
qDebug()<<"HelloWorld ->toUpper() : "<<abc.toUpper();
QByteArray strNum="-123456";
QByteArray strNumU="123456";
qDebug()<<"strNum ->toInt() : "<<strNum.toInt();
qDebug()<<"strNum ->toUInt() : "<<strNumU.toUInt();
bool isBase=false;
QByteArray strDigit="255";
QByteArray strBin="11111111";
qDebug()<<"digit ->toInt() base on 10 : "<<strDigit.toInt(&isBase)<<",isBase:"<<isBase;
qDebug()<<"digit ->toInt() base on 16 : "<<strDigit.toInt(&isBase,16)<<",isBase:"<<isBase;
qDebug()<<"digit ->toInt() base on 8 : "<<strDigit.toInt(&isBase,8)<<",isBase:"<<isBase;
qDebug()<<"digit ->toInt() base on 2 : "<<strBin.toInt(&isBase,2)<<",isBase:"<<isBase;
QByteArray strDouble="3.14159";
qDebug()<<"strDouble ->toDouble() : "<<strDouble.toDouble(&isBase)<<",isBase:"<<isBase;
qDebug()<<"strDouble ->toFloat() : "<<strDouble.toFloat(&isBase)<<",isBase:"<<isBase;
qDebug()<<QByteArray::number(strDouble.toDouble());
qDebug()<<QByteArray::number(strDouble.toDouble(),'g');
qDebug()<<QByteArray::number(strDouble.toDouble(),'g',3);
qDebug()<<QByteArray::number(strDouble.toDouble(),'G');
qDebug()<<QByteArray::number(strDouble.toDouble(),'G',3);
qDebug()<<QByteArray::number(strDouble.toDouble(),'f');
qDebug()<<QByteArray::number(strDouble.toDouble(),'f',3);
qDebug()<<QByteArray::number(strDouble.toDouble(),'e');
qDebug()<<QByteArray::number(strDouble.toDouble(),'e',3);
qDebug()<<QByteArray::number(strDouble.toDouble(),'E');
qDebug()<<QByteArray::number(strDouble.toDouble(),'E',3);
}
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();
test_datatype_convert();
return a.exec();
}