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 convert a boost::multiprecision::cpp_int to QByteArray
Forum Updated to NodeBB v4.3 + New Features

How to convert a boost::multiprecision::cpp_int to QByteArray

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 1.1k Views 3 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.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by aha_1980
    #1

    How can I convert a cpp_int to a binary QByteArray? I already convert a quint32 to a QByteArray with the following function:

    QByteArray MySmallNumber::intToByteArray(QDataStream::ByteOrder byteOrder)
    {
        // Convert the int to QByteArray
        QByteArray intByteArray;
        QDataStream intDataStream(&intByteArray, QIODevice::WriteOnly);
    
        // The default setting is big endian.
        // We recommend leaving this setting unless you have special requirements.
        //intDataStream.setByteOrder(m_byteOrder);
        intDataStream.setByteOrder(byteOrder);
        intDataStream << m_intNumber;
    
        return intByteArray;
    }
    
    K 1 Reply Last reply
    0
    • I Infinity

      How can I convert a cpp_int to a binary QByteArray? I already convert a quint32 to a QByteArray with the following function:

      QByteArray MySmallNumber::intToByteArray(QDataStream::ByteOrder byteOrder)
      {
          // Convert the int to QByteArray
          QByteArray intByteArray;
          QDataStream intDataStream(&intByteArray, QIODevice::WriteOnly);
      
          // The default setting is big endian.
          // We recommend leaving this setting unless you have special requirements.
          //intDataStream.setByteOrder(m_byteOrder);
          intDataStream.setByteOrder(byteOrder);
          intDataStream << m_intNumber;
      
          return intByteArray;
      }
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Infinity

      Since cpp_int is apparently supporting output operator<< according to this here, you could go through std::ostringstream.

      Possibly there is a more elegant possibility.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        You already had a conversion in your previous post some days ago - did it not work?
        https://forum.qt.io/topic/111466/convert-64-byte-qbytearray-to-two-32-byte-int/8

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        I 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          You already had a conversion in your previous post some days ago - did it not work?
          https://forum.qt.io/topic/111466/convert-64-byte-qbytearray-to-two-32-byte-int/8

          I Offline
          I Offline
          Infinity
          wrote on last edited by Infinity
          #4

          @Christian-Ehrlicher The code in my previous post worked, but I this time I wanted to have it in binary format. It sometimes helps me to ask questions to find the right solution :-)

          Now I came up with the following solution:

          QByteArray MyBigNumber::byteArrayHex()
          {
              // See https://www.systutorials.com/131/convert-string-to-int-and-reverse/
          
              // Convert the cpp_int to a hex std::string
              std::string str;
              std::stringstream stream;
              stream << std::hex << m_cppInt;
              str = stream.str();
          
              // Convert the hex std::string to a binary QByteArray
              return QByteArray::fromHex(QByteArray::fromStdString(str));
          }
          

          How does that look like?

          kshegunovK 1 Reply Last reply
          0
          • I Infinity

            @Christian-Ehrlicher The code in my previous post worked, but I this time I wanted to have it in binary format. It sometimes helps me to ask questions to find the right solution :-)

            Now I came up with the following solution:

            QByteArray MyBigNumber::byteArrayHex()
            {
                // See https://www.systutorials.com/131/convert-string-to-int-and-reverse/
            
                // Convert the cpp_int to a hex std::string
                std::string str;
                std::stringstream stream;
                stream << std::hex << m_cppInt;
                str = stream.str();
            
                // Convert the hex std::string to a binary QByteArray
                return QByteArray::fromHex(QByteArray::fromStdString(str));
            }
            

            How does that look like?

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            boost's multiprecision keeps the data in tuples of binary, but you can use bitshifts to just extract it byte by byte (as with any other number). Something along these lines:

            typedef decltype(m_cppInt) BigInt;
            constexpr int bytes = std::numeric_limits<BigInt>::digits / 8;
            constexpr quint8 mask = quint8(-1);
            constexpr quint8 shift = sizeof(quint8) * 8;
            
            QByteArray data(bytes, 0);
            for (int i = 0; i < bytes; i++)  {
                data[bytes - i - 1] = quint8(m_cppInt & mask);
                m_cppInt >>= shift;
            }
            

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            1
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              boost multiprecision can serialise/deserialise itself.

              std::vector<char> chars;
              boost::iostreams::stream_buffer<boost::iostreams::back_insert_device<std::vector<char> > > bb(chars);
              boost::archive::binary_oarchive oa(bb, boost::archive::no_header | boost::archive::no_tracking | boost::archive::no_codecvt);
              oa << m_cppInt;
              QByteArray serialised(chars.data(),chars.size());
              

              See https://stackoverflow.com/questions/50465041/how-to-convert-boostmultiprecisioncpp-int-tofrom-an-array-of-byte

              P.S.
              boost::iostreams::back_insert_device might work directly with QByteArray as well without the need to use the std::vector<char> indirection but I have no way of testing it atm, feel free to explore that option

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              6

              • Login

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