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

QByteArray : Converts data in bytes.



  • Hello everyone.

    I'm having trouble converting to Hex.
    First is the code.

        /* Error Code */
        int n1 = 1;
        QByteArray val1 = QByteArray("1B33")+QByteArray::number(n1,16); // val1 : 1B331
        qDebug() << QByteArray::fromHex(val1); //result : "\x01\xB3\x31" ( I do not want this value )
        // I want result : "\x1B\x33\x01"
    
        /*Good work Code*/
        int n2 = 20;
        QByteArray val2 = QByteArray("1B33")+QByteArray::number(n2,16); // val2 : 1B3314
        qDebug() << QByteArray::fromHex(val2); // result : "\x1B\x33\x14"
    
    

    If the Hex value of 'int n' is a single digit,
    I get results that I did not want.
    I want to store the data in bytes.
    How do I solve this problem?

    I thank you in advance for your advice.



  • @Kycho

    The method varies, but the method by Qt only is something like this.

    QByteArray val1 = QByteArray("1B33")+QByteArray::number(n1,16).rightJustified(2, '0');
    

Log in to reply