字节数组类QByteArray提供一个字节数组用于存储原始字节,号称 Best array!它比使用char *更方便,该类在串口通信中经常被使用,因为串口通信数据都是一个一个的8位字节流。
1、初始化
1.构造函数初始化方式;
QByteArray ba(“Hello”);
QByteArray array("Hello");
if('o'==array[4])
{
qInfo()<<"array[4]='o'";
}
虽然size()为5,但是字节数组在最后还会保留一个额外的’\ 0’字符, 以便如果使用一个函数来请求指向底层数据的指针(例如调用data()),那么指出的数据保证被’\ 0’终止。
以下代码,是无法编译通过的,因为索引是小于size的。加\0,不是说size就变6了。
QByteArray array("Hello");
qInfo()<<"array.size="<<array.size();//5
if('\0'==array[5])
{
qInfo()<<"array[5]='\0'";
}
//无法编译通过,报错,索引不允许等于size!
2、逐个元素初始化
QByteArray ba;
ba.resize(3);
ba[0] = 0x3c;
ba[1] = 0xb8;
ba[2] = 0x64;
2、访问某个元素
主要四种方式:
[ ] 和 data[ ] 为可读可写
at( ) 和 constData[ ]仅为可读 //如果只读采用这种方式避免了深度复制更快;
3、截取字符串
函数left()、right()或mid() ;
4、获取数组大小
函数size、length和count,完全等同;
5、数据转换与处理
1. Hex转换(十六进制转换)
这在调试特别有用,因为大多Hex码没有字符显示
QByteArray hex = data.toHex();
qInfo() << "Hex" << hex;
QByteArray from_hex = QByteArray::fromHex(hex);
qInfo() << "From Hex" << from_hex;
2、数值转换
QByteArray number(int n, int base = 10)
其中,参数n是要转变的整数;base是要进行转换的进制,进制取值范围为2到36,
即从二进制到三十六进制。该函数返回整数n对应的base进制的字符数组。下列代码演示
了number ()函数的使用:
int n=63;
qInfo()<<QByteArray::number(n);//63
qInfo()<<QByteArray::number(n,2);//111111
qInfo()<<QByteArray::number(n,8);//77
qInfo()<<QByteArray::number(n,16);//3f
6、大小写转换及判断
函数toUpper()和toLower()
函数isUpper和isLower
7、字符串数值转为各类数值
toint() tofloat() todouble()等
8、QByteArray与char*互转
1、char*转QByteArray
char *ch;
QByteArray byte;
byte = QByteArray(ch);
2、QByteArray转char*
char *b=a.data();
9、QByteArray与std::string互转
std::string toStdString();
QByteArray::fromStdString(const std::string &str);
10、与字符串QString互转
QString str=QString("hello world!");
QByteArray arr = str.toLatin1();
QByteArray arr("hello world!");
QString str = arr;
11、QByteArray与自定义结构体之间的转化
#include <QByteArray>
#include <QDebug>
#include <stdlib.h>
typedef struct Header{
int channel;
int type;
}Headr;
typedef struct Msg{
Header header;
char content[128];
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;
friend QDebug operator << (QDebug os, PeerMsg msg){
os << "( " << " ipV4:" << QString::number(msg.ipV4)
<< " port:" << QString::number(msg.port)
<< " )";
return os;
}
}PeerMsg;
int main(void)
{
Msg msg;
msg.header.channel = 1001;
msg.header.type = 1;
strcpy(msg.content, "ABCDEFG");
qDebug() << msg;
QByteArray array;
array.append((char*)&msg, sizeof(msg));
Msg *getMsg = (Msg*)array.data();
qDebug() << *getMsg;
QByteArray totalByte;
PeerMsg peerMsg(123456, 10086);
totalByte.append((char*)&peerMsg, sizeof(PeerMsg));
totalByte.append(array, array.size());
PeerMsg *getByte = (PeerMsg*)totalByte.data();
qDebug() << *getByte;
QByteArray contentmsg = totalByte.right(totalByte.size() - sizeof(*getByte));
Msg *getMsg2 = (Msg*)contentmsg.data();
qDebug() << *getMsg2;
return 0;
}
12、判断是否为空
bool isEmpty();
13、向前搜索和向后搜索
int indexOf(const QByteArray &ba, int from = 0);
QByteArray b("qwertyuitop");
b.indexOf("t");
b.indexOf("t",1);
14、插入
函数insert()可以在某个索引位置上插入:
QByteArray & insert(int i, const QByteArray &ba);
QByteArray ba("Meal");
ba.insert(1, QByteArray("ontr"));
// ba == "Montreal"
15、Hex和base64
QByteArray hex = data.toHex();
qInfo() << "Hex" << hex;
QByteArray from_hex = QByteArray::fromHex(hex);
qInfo() << "From Hex" << from_hex;
QByteArray base64 = data.toBase64();
qInfo() << "base64" << base64;
QByteArray from_base64 = QByteArray::fromBase64(base64);
qInfo() << "From base64" << from_base64;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/96481.html