Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to load data into QBitArray
Forum Updated to NodeBB v4.3 + New Features

How to load data into QBitArray

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 3.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koliva
    wrote on last edited by
    #1

    Hi,
    I receive image frames with a frame header of 128 bit. I wanted to use QBitArray for the header part but I couldn't manage memcopy the data from the received buffer to QBitArray. Could anyone help me out?

    Here is what I roughly have:

    QBitArray myBitArray;
    memcpy(myBitArray.data_ptr(), myPixelBuffer, 16); //16 = 128bit
    myPixelBuffer = myPixelBuffer + 16; //Starting the buffer right after the header.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Don't you mean a QByteArray ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tarod.net
        wrote on last edited by
        #3

        I think QBitArray doesn't have a method called "data_ptr()". You mean "data()"?

        Could you use QByteArray and its constructor? Only for checking the differente between your code and using QByteArray.

        QByteArray::QByteArray(const char * data, int size = -1)
        

        "Individually, we are one drop. Together, we are an ocean."

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koliva
          wrote on last edited by
          #4

          Hi,
          I could also do it with QByteArray, however I need to do bit-wise operations on 128 bits altogether. So, I thought if I put them in QBitArray, I can shift them all easily. Do you have any suggestions?

          T 1 Reply Last reply
          0
          • K koliva

            Hi,
            I could also do it with QByteArray, however I need to do bit-wise operations on 128 bits altogether. So, I thought if I put them in QBitArray, I can shift them all easily. Do you have any suggestions?

            T Offline
            T Offline
            tarod.net
            wrote on last edited by
            #5

            @koliva Well, you can get a pointer to the data stored in the byte array and use this pointer to do the operations.

            Example:

            QByteArray ba("Hello world");
            char *data = ba.data();
            while (*data) {
                cout << "[" << *data << "]" << endl;
                ++data;
            }
            

            The idea is to use the QByteArray constructor to initialize the object with your buffer of pixels.

            "Individually, we are one drop. Together, we are an ocean."

            1 Reply Last reply
            1
            • K Offline
              K Offline
              koliva
              wrote on last edited by
              #6

              Can you also give me an example how to mask some part of the 128 bit?
              For example, how can I construct a mask which gives me the bits in between, for example, 48 and 71?

              1 Reply Last reply
              0
              • O Offline
                O Offline
                onek24
                wrote on last edited by onek24
                #7

                Can you also give me an example how to mask some part of the 128 bit?
                For example, how can I construct a mask which gives me the bits in between, for example, 48 and 71?

                @koliva It's quite simple if you use 'bitwise shifting' and 'bitwise and'. You might check out this Website. You can mask using '&' and shift it to align using '>>' or '<<'. The only problem i see here is that your processor can't quite use logic on anything higher than it's word size, which is 32 or 64 bit in modern general purpose computers. So using QBitArray doesn't seem wrong for me. If you want to get a certain area of bits from your QBitArray, you might want to use the operator[] or QByteArray::at(). Example(not tested):

                QBitArray array(128); # Initialize a new 128-bit array
                ...
                # manipulate our bitarray like we want
                ...
                # from 48 to 71
                int from = 48;
                int to = 71;
                for (int i = from; i < to; ++i)
                {
                    bool bitAtI = array.at(i);
                    # or use the operator [] to get a MODIFIABLE REFERENCE of the bit!
                    # bool bitAtI = array[i];
                }
                

                Using this approach you will get each bit from 48 to 71. Write them out into a new QBitArray or do whatever you want with them.
                For further informations about QBitArray read: QBitArray

                One more IMPORTANT thing:

                The approach using QBitArray::at() from above will give you just a copy of the bit! If you use the operator [], you will get a modifiable reference of the bit in your QBitArray!

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved