1:QBitArray2QString :也可以转化为整型,
测试程序: 测试输出结果是否和移位结果相同;
[cpp] view plaincopyprint?
- 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 plaincopyprint?
- 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 plaincopyprint?
- 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 plaincopyprint?
- QByteArray z;
- z.append(0x68).append(0x65).append(0x6c).append(0x6c).append(0x6f);
- QString::fromAscii(z.data());
- //把QByteArray转化成QString,
输出结果:hello
3:QByteArray 转化为整型:这里是对位操作,用处很大
[cpp] view plaincopyprint?
- 头文件包含:
- 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 plaincopyprint?
- 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 plaincopyprint?
- 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
4: 如果希望QByteArray转化为QBitArray的话,可以尝试第三种方法。个人觉得QBitArray主要是对位操作,如果有兴趣的朋友,可以写写看。
时间: 2024-10-04 20:59:21