怎么将int转换为qbytearray

 我来答
猫奴努生BM
2017-08-02 · TA获得超过127个赞
知道小有建树答主
回答量:599
采纳率:0%
帮助的人:310万
展开全部
1:QBitArray2QString :也可以转化为整型,

测试程序: 测试输出结果是否和移位结果相同;

[cpp]
view plaincopy

QBitArray x;
int bit;
bit = 10;
x.resize(32);

x.fill(false);
x.setBit(bit,true);
QBitArray b;
b = this->BitArrayInvert(x);
ui->textEdit->setPlainText(this->bitArray2String(b).toAscii());
qDebug() << (0x1 << bit) <<this->bitArray2String(b)<< __FILE__<<__LINE__;

[cpp] view plain copy
QBitArray x;
int bit;
bit = 10;
x.resize(32);

x.fill(false);
x.setBit(bit,true);
QBitArray b;
b = this->BitArrayInvert(x);
ui->textEdit->setPlainText(this->bitArray2String(b).toAscii());
qDebug() << (0x1 << bit) <<this->bitArray2String(b)<< __FILE__<<__LINE__;

倒置程序:

[cpp]
view plaincopy

QBitArray BitArray2String::BitArrayInvert(QBitArray array)
{
QBitArray temp;
temp.resize(array.size());
temp.fill(false);
for ( int i = 0; i < array.size(); ++i ) {
if ( array.testBit(i) ) {
temp.setBit(array.size()-1-i,true);
}
}
return temp;
}

[cpp] view plain copy
QBitArray BitArray2String::BitArrayInvert(QBitArray array)
{
QBitArray temp;
temp.resize(array.size());
temp.fill(false);
for ( int i = 0; i < array.size(); ++i ) {
if ( array.testBit(i) ) {
temp.setBit(array.size()-1-i,true);
}
}
return temp;
}

转化程序:

[cpp]
view plaincopy

QString BitArray2String::bitArray2String(QBitArray array)
{
uint value = 0;
for ( uint i = 0; i < array.size(); ++i ) {
value <<= 1;
value += (int)array.at(i);
}
qDebug() << value <<__FILE__<<__LINE__; //转化出的整形

QString str;
str.setNum(value, 10);
return str;
}

[cpp] view plain copy
QString BitArray2String::bitArray2String(QBitArray array)
{
uint value = 0;
for ( uint i = 0; i < array.size(); ++i ) {
value <<= 1;
value += (int)array.at(i);
}
qDebug() << value <<__FILE__<<__LINE__; //转化出的整形
QString str;
str.setNum(value, 10);
return str;
}

输出结果: 1024 “1024”

2:QByerArray 2 QString

[cpp]
view plaincopy

QByteArray z;
z.append(0x68).append(0x65).append(0x6c).append(0x6c).append(0x6f);

QString::fromAscii(z.data());

//把QByteArray转化成QString,

[cpp] view plain copy
QByteArray z;
z.append(0x68).append(0x65).append(0x6c).append(0x6c).append(0x6f);

QString::fromAscii(z.data());

//把QByteArray转化成QString,

输出结果:hello

3:QByteArray 转化为整型:这里是对位操作,用处很大

[cpp]
view plaincopy

头文件包含:
typedef unsigned long U32;
程序如下:
QByteArray z;
z.resize(1);
z[0] = 0x0f;

U32 U1_JZPL_set;
U1_JZPL_set = 0;
U1_JZPL_set |= ((U32) z.at(0)) ;
qDebug()<<"/nU1_JZPL_set" <<U1_JZPL_set;

[cpp] view plain copy
头文件包含:
typedef unsigned long U32;
程序如下:
QByteArray z;
z.resize(1);
z[0] = 0x0f;

U32 U1_JZPL_set;
U1_JZPL_set = 0;
U1_JZPL_set |= ((U32) z.at(0)) ;
qDebug()<<"/nU1_JZPL_set" <<U1_JZPL_set;

输出结果: 15

QByteArray
转int() 直接转化

[cpp]
view plaincopy

int bytesToInt(QByteArray bytes) {
int addr = bytes[0] & 0x000000FF;
addr |= ((bytes[1] << 8) & 0x0000FF00);
addr |= ((bytes[2] << 16) & 0x00FF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
return addr;
}

[cpp] view plain copy
int bytesToInt(QByteArray bytes) {
int addr = bytes[0] & 0x000000FF;
addr |= ((bytes[1] << 8) & 0x0000FF00);
addr |= ((bytes[2] << 16) & 0x00FF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
return addr;
}

int转QByteArray

[cpp]
view plaincopy

QByteArray intToByte(int number)
{
QByteArray abyte0;
abyte0.resize(4);
abyte0[0] = (uchar) (0x000000ff & number);
abyte0[1] = (uchar) ((0x0000ff00 & number) >> 8);
abyte0[2] = (uchar) ((0x00ff0000 & number) >> 16);
abyte0[3] = (uchar) ((0xff000000 & number) >> 24);
return abyte0;
}

[cpp] view plain copy
QByteArray intToByte(int number)
{
QByteArray abyte0;
abyte0.resize(4);
abyte0[0] = (uchar) (0x000000ff & number);
abyte0[1] = (uchar) ((0x0000ff00 & number) >> 8);
abyte0[2] = (uchar) ((0x00ff0000 & number) >> 16);
abyte0[3] = (uchar) ((0xff000000 & number) >> 24);
return abyte0;
}

qDebug()
<< intToByte(252);

输出结果:FC
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式