Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to convert int into qbytearray
How to convert int into qbytearray
1: qbitarray2qstring: It can also be converted into an integer.

Testing step: testing whether the output result is the same as the shift result;

[cpp]

View plain text

QBitArray x;

Int bit;

bit = 10;

x . resize(32);

x . fill(false);

x.setBit(bit,true);

QBitArray b;

b = this-& gt; BitArrayInvert(x);

ui-& gt; Text editing-> setPlainText(this-& gt; bitArray2String(b)。 toa scii());

qDebug()& lt; & lt(0x 1 & lt; & ltbit)& lt; & lt This-> bitarray 2 string(b)& lt; & lt_ _ FILE _ _ & lt& lt_ _ LINE _ _

[cpp] View plain text

QBitArray x;

Int bit;

bit = 10;

x . resize(32);

x . fill(false);

x.setBit(bit,true);

QBitArray b;

b = this-& gt; BitArrayInvert(x);

ui-& gt; Text editing-> setPlainText(this-& gt; bitArray2String(b)。 toa scii());

qDebug()& lt; & lt(0x 1 & lt; & ltbit)& lt; & lt This-> bitarray 2 string(b)& lt; & lt_ _ FILE _ _ & lt& lt_ _ LINE _ _

Inversion procedure:

[cpp]

View plain text

Qbit array bitarraystring:: bitarrayinvert (qbit array)

{

QBitArray temperature;

temp . resize(array . size());

temp . fill(false);

for(int I = 0; I< array.size (); ++i ) {

if ( array.testBit(i) ) {

temp . set bit(array . size()- 1-I,true);

}

}

Return temperature;

}

[cpp] View plain text

Qbit array bitarraystring:: bitarrayinvert (qbit array)

{

QBitArray temperature;

temp . resize(array . size());

temp . fill(false);

for(int I = 0; I< array.size (); ++i ) {

if ( array.testBit(i) ) {

temp . set bit(array . size()- 1-I,true);

}

}

Return temperature;

}

Transformation plan:

[cpp]

View plain text

Qstring bitarraystring:: bitarraystring (qbit array array)

{

Uint value = 0;

for(uint I = 0; I< array.size (); ++i ) {

Value < < =1;

value+=(int)array . at(I);

}

qDebug()& lt; & lt value & lt _ _ file _ _< _ _ line _//transformation and shaping.

QString string;

Str.setNum (value,10);

Returns a string;

}

[cpp] View plain text

Qstring bitarraystring:: bitarraystring (qbit array array)

{

Uint value = 0;

for(uint I = 0; I< array.size (); ++i ) {

Value < < =1;

value+=(int)array . at(I);

}

qDebug()& lt; & lt value & lt _ _ file _ _< _ _ line _//transformation and shaping.

QString string;

Str.setNum (value,10);

Returns a string;

}

Output result: 1024 "1024"

2:QByerArray 2 QString

[cpp]

View plain text

QByteArray z;

z.append(0x68)。 Added (0x65). Added (0x6c). Added (0x6c). Added (0x6f);

QString::from ascii(z . data());

//convert QByteArray into QString,

[cpp] View plain text

QByteArray z;

z.append(0x68)。 Added (0x65). Added (0x6c). Added (0x6c). Added (0x6f);

QString::from ascii(z . data());

//convert QByteArray into QString,

Output result: hello

3.Q ByteArray is converted into Integer: This is a contraposition operation, which is very useful.

[cpp]

View plain text

The header file contains:

Typedef unsigned long U32

The process is as follows:

QByteArray z;

z . resize( 1);

z[0]= 0x0f;

u32 u 1 _ JZPL _ set;

u 1 _ JZPL _ set = 0;

u 1 _ JZPL _ set | =((U32)z . at(0));

qDebug()& lt; & lt"/nu 1 _ JZPL _ set " & lt; & ltu 1 _ JZPL _ set;

[cpp] View plain text

The header file contains:

Typedef unsigned long U32

The process is as follows:

QByteArray z;

z . resize( 1);

z[0]= 0x0f;

u32 u 1 _ JZPL _ set;

u 1 _ JZPL _ set = 0;

u 1 _ JZPL _ set | =((U32)z . at(0));

qDebug()& lt; & lt"/nu 1 _ JZPL _ set " & lt; & ltu 1 _ JZPL _ set;

Output result: 15

QByteArray

Direct conversion from int ()

[cpp]

View plain text

int bytesToInt(QByteArray bytes){

int addr = bytes[0]& amp; 0x000000FF

addr | =((bytes[ 1]& lt; & lt8)& amp; 0x 0000 ff 00);

addr | =((bytes[2]& lt; & lt 16); 0x 00ff 0000);

addr | =((bytes[3]& lt; & lt24)& amp; 0x ff 000000);

Return address;

}

[cpp] View plain text

int bytesToInt(QByteArray bytes){

int addr = bytes[0]& amp; 0x000000FF

addr | =((bytes[ 1]& lt; & lt8)& amp; 0x 0000 ff 00);

addr | =((bytes[2]& lt; & lt 16); 0x 00ff 0000);

addr | =((bytes[3]& lt; & lt24)& amp; 0x ff 000000);

Return address;

}

Int to QByteArray

[cpp]

View plain text

QByteArray integer bytes (integer)

{

QByteArray abyte0

abyte 0 . resize(4);

abyte 0[0]=(uchar)(0x 000000 ff & amp; No.);

abyte 0[ 1]=(uchar)((0x 0000 ff 00 & amp; No.) >; & gt8);

abyte 0[2]=(uchar)((0x 00 ff 0000 & amp; No.) >; & gt 16);

abyte 0[3]=(uchar)((0x ff 000000 & amp; No.) >; & gt24);

return abyte0

}

[cpp] View plain text

QByteArray integer bytes (integer)

{

QByteArray abyte0

abyte 0 . resize(4);

abyte 0[0]=(uchar)(0x 000000 ff & amp; No.);

abyte 0[ 1]=(uchar)((0x 0000 ff 00 & amp; No.) >; & gt8);

abyte 0[2]=(uchar)((0x 00 ff 0000 & amp; No.) >; & gt 16);

abyte 0[3]=(uchar)((0x ff 000000 & amp; No.) >; & gt24);

return abyte0

}

qDebug()

& lt& ltintToByte(252);

Output result: FC