Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to change the byte order of a boost::multiprecision::cpp_int and store in a QByteArry



  • I have to change the byte order of a boost::multiprecision::cpp_int and store it in a QByteArry.

    That is how I do it for quint64:

    QByteArray MyLongNumber::intToByteArray(QDataStream::ByteOrder byteOrder)
    {
        // Convert the int to bianry QByteArray according to byteOrder (QDataStream::BigEndian | QDataStream::LittleEndian)
    
        QByteArray intByteArray;
        QDataStream intDataStream(&intByteArray, QIODevice::WriteOnly);
    
        intDataStream.setByteOrder(byteOrder);
        intDataStream << m_intNumber;
    
        return intByteArray;
    }
    

    How can I do it for a boost::multiprecision::cpp_int. I tried it like this:

    QByteArray MyBigNumber::cppIntToByteArray(QDataStream::ByteOrder byteOrder)
    {
        // Convert the int to bianry QByteArray according to byteOrder (QDataStream::BigEndian | QDataStream::LittleEndian)
    
        boost::multiprecision::cpp_int m_cppInt(4000000000000000);
    
        QByteArray intByteArray;
        QDataStream intDataStream(&intByteArray, QIODevice::WriteOnly);
    
        intDataStream.setByteOrder(byteOrder);
        intDataStream << m_cppInt;
    
        return intByteArray;
    }
    

    But that doesn't work. How can I do that?


  • Lifetime Qt Champion

    Hi,

    In what does it not work ?
    On what OS ?
    With which compiler ?



  • How/why would QDataStream know anything about boost::multiprecision?

    They are from different frameworks, so unless the intDataStream is taught how to handle
    intDataStream << m_cppInt
    the operation is going to be undefined. I think you'll need to look under the hood to see how boost stores its mutliprecision types before making any assumptions about compatibility with Qt.


Log in to reply