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. What is the best way to cast Quint16 to QBitarray(16) ?
QtWS25 Last Chance

What is the best way to cast Quint16 to QBitarray(16) ?

Scheduled Pinned Locked Moved Solved General and Desktop
qbitarraycasting
4 Posts 3 Posters 2.1k Views
  • 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.
  • Z Offline
    Z Offline
    zeroptr
    wrote on 26 Jan 2016, 20:12 last edited by
    #1

    Hi all,

    What is the best way to cast Quint16 to QBitarray(16) ?

    thanx..

    Linux Mint 20.04 64 Bit QT6.0.1

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 26 Jan 2016, 23:45 last edited by
      #2

      Hi!
      I don't think there's a 'nice' way. So maybe something like this:

      QBitArray quint16ToQBitArray(quint16 v)
      {
          QBitArray ba(16);
          for (int i=0; i<16; i++)
              ba.setBit(i, v>>i & 1);
          return ba;
      }
      
      
      quint16 qBitArrayToQuint16(const QBitArray &ba)
      {
          quint16 v = 0;
          for (int i=0; i<16; i++)
              v |= ba.at(i)<<i;
          return v;
      }
      
      K 1 Reply Last reply 27 Jan 2016, 00:01
      2
      • ? A Former User
        26 Jan 2016, 23:45

        Hi!
        I don't think there's a 'nice' way. So maybe something like this:

        QBitArray quint16ToQBitArray(quint16 v)
        {
            QBitArray ba(16);
            for (int i=0; i<16; i++)
                ba.setBit(i, v>>i & 1);
            return ba;
        }
        
        
        quint16 qBitArrayToQuint16(const QBitArray &ba)
        {
            quint16 v = 0;
            for (int i=0; i<16; i++)
                v |= ba.at(i)<<i;
            return v;
        }
        
        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 27 Jan 2016, 00:01 last edited by kshegunov
        #3

        Hello,
        If you feel lazy and don't want to run for loops, as I usually do, you could also try:

        quint16 value; //< Your value
        
        QByteArray data = QByteArray::fromRawData(reinterpret_cast<char *>(&value), sizeof(quint16));
        QDataStream stream(&data, QIODevice::ReadOnly);
        
        QBitArray bitArray(sizeof(quint16));
        stream >> bitArray;
        

        Although it doesn't seem to be shorter than @Wieland's suggestion.

        Kind regards.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zeroptr
          wrote on 27 Jan 2016, 16:43 last edited by
          #4

          Hi,

          @kshegunov I tried the same thing also your code but I could not manage it to work.. I have already have a Qdatastream, I get quint16 values from it... I tried your solution at first sight...

          @Wieland The code you send works fine thank you very much..

          Have Nice day guys.. Thanks for your help.. It's very valuable..

          Linux Mint 20.04 64 Bit QT6.0.1

          1 Reply Last reply
          0

          1/4

          26 Jan 2016, 20:12

          • Login

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