1:qbitarray2qstring: can also be converted to integral type,
Test program: Test output is the same as the displacement results;
[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__;
Inverted program:
[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;
- }
Conversion program:
[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__; //transformation of plastic
- qstring str;
- str.setnum (value, 10);
- return str;
- }
Output Result: 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 ());
- Turn Qbytearray into Qstring,
Output Result: Hello
3:qbytearray to Integer: This is a bitwise operation, it's very useful.
[CPP]View Plaincopyprint?
- The header file contains:
- typedef unsigned long U32;
- The procedure is as follows:
- 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;
Output results: 15
Qbytearray to int () direct conversion
[CPP]View Plaincopyprint?
- int bytestoint (qbytearray bytes) {
- int addr = bytes[0] & 0x000000ff;
- Addr |= ((Bytes[1] << 8) & 0x0000FF00);
- Addr |= ((bytes[2] <<) & 0x00ff0000);
- Addr |= ((Bytes[3] <<) & 0xff000000);
- return addr;
- }
int turn 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);
Output Result: FC
4: If you want Qbytearray to be converted to Qbitarray, you can try a third method. Personally think Qbitarray is mainly on the bit operation, if interested in friends, can write to see.
Conversion between Qstring,qbytearray and Qbitarray