初始化
#include "myqbytearray.h"
#include "ui_myqbytearray.h"
#include "QDebug"
#include "QtGlobal"
MyQByteArray::MyQByteArray(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyQByteArray)
{
ui->setupUi(this);
qDebug()<<"字節(jié)數(shù)組類的初始化";
QByteArray ba("hello");
if('\0'==ba[5]){
printf("ba[5]=\'\\0\'\n");
}
QByteArray ba2("china");
ba2.fill('q');
qDebug()<<ba2;
if('\0'==ba2[5]){
printf("ba2[5]=\'\\0\'\n");
}
ba2.fill('x',2);
qDebug()<<ba2;
if('\0'==ba[5]){
printf("new : ba2[5]=\'\\0\'\n");
}
}
MyQByteArray::~MyQByteArray()
{
delete ui;
}
字節(jié)數(shù)組類以 ‘\0’結(jié)尾,索引的下標(biāo)從0開始。
第一次調(diào)用fill函數(shù),不指定size參數(shù),按照之前的長度,跟新值
第二次調(diào)用fill函數(shù),指定size參數(shù),重新調(diào)整字節(jié)數(shù)組的長度,并更新值
訪問某個(gè)元素
訪問QByteArray類對(duì)象的某個(gè)元素有4種方式:
- []
- at()
- data[]
- constData[]
其中,[]和data[]可讀可寫,at()和constData[]只讀、
MyQByteArray2::MyQByteArray2(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyQByteArray2)
{
ui->setupUi(this);
QByteArray ba1("hello");
ba1.resize(10);
ba1[0] = 'a';
ba1[1] = 'b';
ba1[2] = 'c';
ba1[3] = 'd';
ba1[4] = 'e';
ba1[5] = 'f';
ba1[6] = 'g';
ba1[7] = 'h';
ba1[8] = 'i';
//ba1[9] = 'j';
qDebug()<<ba1;
if('\0'==ba1[10]){
printf("ba1[10] = \'\\0\'\n");
}
ba1[1]='a';
ba1.data()[2] ='a';
qDebug()<<ba1;
qDebug()<<ba1.at(1);
qDebug()<<ba1.constData()[2];
qDebug()<<ba1.constData()[3];
}
截取字符串
使用函數(shù) left() right() mid()來提取多個(gè)字節(jié)。
MyQByteArray3::MyQByteArray3(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyQByteArray3)
{
ui->setupUi(this);
QByteArray x("pineapple");
QByteArray y1 = x.left(4);
qDebug()<<y1;
QByteArray y2 = x.right(5);
qDebug()<<y2;
QByteArray y3 = x.mid(2,4);
qDebug()<<y3;
QByteArray y4 = x.mid(2);
qDebug()<<y4;
}
獲取字節(jié)數(shù)組的大小
成員函數(shù)size(),length(),count()來獲得字節(jié)數(shù)組的大小(除了名字不同,這3個(gè)函數(shù)是相同的)
- int size()
- int length()
- int count()
size()不包括字符串末尾添加的 ‘\0’
如果以字符串形式初始化,中間有‘\0’,則不統(tǒng)計(jì)后面的字符。
如果改變中間的字符,為‘\0’,并不會(huì)被size()截?cái)?/p>
MyQByteArray4::MyQByteArray4(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyQByteArray4)
{
ui->setupUi(this);
QByteArray b4("hello");
int n1 = b4.size();
int n2 = b4.size();
int n3 = b4.size();
qDebug()<<n1<<" "<<n2<<" "<<n3<<" ";
b4.data()[3]='\n';
qDebug()<<b4.size();
QByteArray b41("he\0llo");
qDebug()<<b41.size();
}
數(shù)據(jù)轉(zhuǎn)換與處理
Hex轉(zhuǎn)換
QByteArray::fromHex() 將16進(jìn)制編碼的數(shù)據(jù)轉(zhuǎn)換為字符類型的數(shù)據(jù)。
.toHex() 將字節(jié)數(shù)組中,字符轉(zhuǎn)換為十六進(jìn)制的數(shù)值編碼。
MyQByteArray5::MyQByteArray5(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyQByteArray5)
{
ui->setupUi(this);
QByteArray text1 = QByteArray::fromHex("517420697320677265617421");
qDebug()<<text1.data();
QByteArray text2 = text1.toHex();
qDebug()<<text2.data();
}
數(shù)值轉(zhuǎn)換與輸出
QByteArray類的公有靜態(tài)函數(shù)number完成十進(jìn)制到其他進(jìn)制的轉(zhuǎn)換。
調(diào)用靜態(tài)成員函數(shù)
int n = 63;
qDebug()<<QByteArray::number(n);
qDebug()<<QByteArray::number(n,16);
qDebug()<<QByteArray::number(n,2);
qDebug()<<QByteArray::number(n,8);
QByteArray類的的公有函數(shù)setNum()
qDebug()<<ba;
ba.setNum(n,16);
qDebug()<<ba;
ba.setNum(n,8);
qDebug()<<ba;
ba.setNum(n,2);
qDebug()<<ba;
QByteArray::number 將小數(shù)按指定格式的小數(shù)位轉(zhuǎn)換輸出。
QByteArray ba1 = QByteArray::number(12345.6,'E',3);
QByteArray ba2 = QByteArray::number(12345.6,'e',3);
QByteArray ba3 = QByteArray::number(12345.6,'f',3);
QByteArray ba4 = QByteArray::number(12345.6,'g',3);
QByteArray ba5 = QByteArray::number(12345.6,'G',3);
qDebug()<<ba1;
qDebug()<<ba2;
qDebug()<<ba3;
qDebug()<<ba4;
qDebug()<<ba5;
字母大小寫轉(zhuǎn)換
函數(shù)toUpper和toLower完成字母大小寫轉(zhuǎn)換。
QByteArray x("hello QT ");
qDebug()<<x.toLower();
qDebug()<<x.toUpper();
字符串?dāng)?shù)值轉(zhuǎn)化為各類數(shù)值
字符數(shù)值轉(zhuǎn)化為各類型數(shù)值:to函數(shù)。
QByteArray strint("1234");
bool ok0;
qDebug()<<strint.toInt();
qDebug()<<strint.toInt(&ok0,16);qDebug()<<ok0;
qDebug()<<strint.toInt(&ok0,8);qDebug()<<ok0;
qDebug()<<strint.toInt(&ok0,2);qDebug()<<ok0; //由于字符串中存在2,3,4等大于2的值,所以不能轉(zhuǎn)化為二進(jìn)制
qDebug()<<"---------";
QByteArray strint2("1234.56");
qDebug()<<strint2.toInt();
qDebug()<<strint2.toFloat();
qDebug()<<strint2.toDouble();
qDebug()<<"---------";
QByteArray str("FF");
bool ok2;
qDebug()<<str.toInt(&ok2,16);
qDebug()<<str.toInt(&ok2,10); //字符串中存在F,超過了10和8進(jìn)制的表示范圍。
qDebug()<<str.toInt(&ok2,8);
QBQyteArray和char*互轉(zhuǎn)
使用.data()成員函數(shù)返回指向字節(jié)數(shù)組中存儲(chǔ)數(shù)據(jù)的指針。
返回一個(gè)指向字節(jié)數(shù)組的指針,指向第一個(gè)字符。
MyQByteArray8::MyQByteArray8(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyQByteArray8)
{
ui->setupUi(this);
QByteArray ba("hello world");
char* data = ba.data();
while(*data){
cout<<" "<<*data;
data++;
}
cout<<endl;
free(data);
}
QByteArray 和std::string互轉(zhuǎn)
QByteArray的類成員函數(shù):toStdString()
QByteArrayd的靜態(tài)成員函數(shù):fromStdString()
MyQByteArray9::MyQByteArray9(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyQByteArray9)
{
ui->setupUi(this);
QByteArray b1("hello");
string s2 = b1.toStdString();
QByteArray b2 = QByteArray::fromStdString(s2);
}
與字符串QString互轉(zhuǎn)
MyQByteArray10::MyQByteArray10(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyQByteArray10)
{
ui->setupUi(this);
QByteArray arr("hello world! ");
QString str = arr;
qDebug()<<str;
qDebug()<<"------------";
QString str2 ("hello");
QByteArray ba = str2.toLatin1();
qDebug()<<ba;
}
QByteArray和自定義結(jié)構(gòu)體之間的轉(zhuǎn)化
定義結(jié)構(gòu)體不能再類中。。。。
根據(jù)char*數(shù)據(jù)和結(jié)構(gòu)體之間的映射,實(shí)現(xiàn)結(jié)構(gòu)體和QByteArray之間的轉(zhuǎn)化。
array.append((char*)&msg,sizeof(msg));
typedef struct Header{
int channel;
int type;
}Header;
typedef struct Msg{
Header header;
char content[128];
//友元函數(shù) operator<<
friend QDebug operator << (QDebug os, Msg msg){
os << "(" << " channel:" << msg.header.channel << " type:" << msg.header.type
<< " content:" << msg.content << " )";
return os;
}
}Msg;
typedef struct PeerMsg
{
PeerMsg(const int &ip,const int &por) {
ipv4 = ip;
port = por;
}
int ipv4;
int port;
//友元函數(shù) operator<<
friend QDebug operator << (QDebug os, PeerMsg msg){
os << "(" << " ipv4:" << QString::number(msg.ipv4) << " port:" << QString::number(msg.port)
<< " )";
return os;
}
}PeerMsg;
void func(){
Msg msg;
msg.header.channel =1000;
msg.header.type=2;
strcpy(msg.content,"hello");
qDebug()<<msg;
PeerMsg peermsg(1921681001,10086);
qDebug()<<peermsg;
qDebug()<<"struct to QByteArray";
QByteArray array;
array.append((char*)&msg,sizeof(msg));
Msg* pMsg =(Msg*)array.data(); //將結(jié)構(gòu)體轉(zhuǎn)換為QByteArray
qDebug()<<*pMsg;
qDebug()<<"QByteArray to struct";
QByteArray totalByte;
totalByte.append((char*)&peermsg,sizeof(peermsg));
PeerMsg* pPeerMsg =(PeerMsg*)totalByte.data(); //將結(jié)構(gòu)體轉(zhuǎn)換為QByteArray
qDebug()<<*pPeerMsg;
}
判斷是否為空
.isEmpty()
MyQByteArray11::MyQByteArray11(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyQByteArray11)
{
ui->setupUi(this);
qDebug()<<QByteArray("").isEmpty();
qDebug()<<QByteArray("ss").isEmpty();
}
向前搜索向后搜索
.indexof() 從前向后
.lastIndexOf 從后向前
QByteArray x("sticky question");
QByteArray y("sti");
qDebug()<<x.indexOf(y);
qDebug()<<x.indexOf(y,6);
qDebug()<<x.indexOf(y,10);
qDebug()<<x.indexOf(y,11);
qDebug()<<x.lastIndexOf(y);
qDebug()<<x.lastIndexOf(y,8);
插入
QByteArray ba("Meal");
ba.insert(2,"hello");
qDebug()<<ba;
文章來源:http://www.zghlxwxcb.cn/news/detail-604749.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-604749.html
到了這里,關(guān)于QT字節(jié)數(shù)組類QByteArray的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!