Conversion between Qstring,qbytearray and Qbitarray

Source: Internet
Author: User

1:qbitarray2qstring: can also be converted to integral type,

Test program: Test output is the same as the displacement results;

[CPP]View Plaincopyprint?
    1. Qbitarray x;
    2. int bit;
    3. bit = 10;
    4. X.resize (32);
    5. X.fill (FALSE);
    6. X.setbit (bit,true);
    7. Qbitarray b;
    8. b = This->bitarrayinvert (x);
    9. Ui->textedit->setplaintext (this->bitarray2string (b). Toascii ());
    10. Qdebug () << (0x1 << bit) <<this->bitarray2string (b) << __file__<<__line__;

Inverted program:

[CPP]View Plaincopyprint?
    1. Qbitarray Bitarray2string::bitarrayinvert (Qbitarray array)
    2. {
    3. Qbitarray temp;
    4. Temp.resize (Array.size ());
    5. Temp.fill (FALSE);
    6. for (int i = 0; i < array.size (); ++i) {
    7. if (Array.testbit (i)) {
    8. Temp.setbit (Array.size () -1-i,true);
    9. }
    10. }
    11. return temp;
    12. }

Conversion program:

[CPP]View Plaincopyprint?
    1. qstring bitarray2string::bitarray2string (Qbitarray array)   
    2. {   
    3.     uint value = 0;  
    4.      for  ( uint i = 0; i < array.size (); ++i )  {  
    5.         value <<= 1;   
    6.         value +=  (int) array.at (i);   
    7.     }  
    8.     qdebug ()  <<  value <<__file__<<__line__;  //transformation of plastic   
    9.      qstring str;  
    10.     str.setnum (value, 10);  
    11.     return str;  
    12. }  

Output Result: 1024 "1024"

2:qbyerarray 2 QString

[CPP]View Plaincopyprint?
    1. Qbytearray Z;
    2. Z.append (0x68). Append (0x65). Append (0x6c). Append (0x6c). Append (0x6f);
    3. Qstring::fromascii (Z.data ());
    4. Turn Qbytearray into Qstring,

Output Result: Hello

3:qbytearray to Integer: This is a bitwise operation, it's very useful.

[CPP]View Plaincopyprint?
    1. The header file contains:
    2. typedef unsigned long U32;
    3. The procedure is as follows:
    4. Qbytearray Z;
    5. Z.resize (1);
    6. Z[0] = 0x0f;
    7. U32 U1_jzpl_set;
    8. U1_jzpl_set = 0;
    9. U1_jzpl_set |= ((U32) z.at (0));
    10. Qdebug () << "/nu1_jzpl_set" <<U1_JZPL_set;

Output results: 15

Qbytearray to int () direct conversion

[CPP]View Plaincopyprint?
    1. int bytestoint (qbytearray bytes) {
    2. int addr = bytes[0] & 0x000000ff;
    3. Addr |= ((Bytes[1] << 8) & 0x0000FF00);
    4. Addr |= ((bytes[2] <<) & 0x00ff0000);
    5. Addr |= ((Bytes[3] <<) & 0xff000000);
    6. return addr;
    7. }

int turn Qbytearray

[CPP]View Plaincopyprint?
    1. qbytearray  inttobyte (int number)     
    2. {     
    3.     qbytearray abyte0;    
    4.     abyte0.resize (4);     
    5.     abyte0[0]  =  (Uchar)    (0x000000ff & number);     
    6.      abyte0[1] =  (Uchar)   ((0x0000ff00 & number)  >> 8 );     
    7.     abyte0[2] =  (Uchar)   ((0x00ff0000  & number)  >> 16)     
    8.      abyte0[3] =  (Uchar)   ((0xff000000 & number)  >> 24);     
    9.     return abyte0;    
    10. }  

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.