Important: Please read the Qt Code of Conduct -

Displaying integer value

  • HI
    I have to take 2 bytes from Qbytearray and display it as signed int in Line edit. is it possible.
    for eg: if my byte 0 is 03 and byte1 is FF--> my data =03ff which is 1023.
    i want to display in line edit as 1023.
    i tried like this
    ui-> value-> setText(QByteArray().append(Buffer[2]).append(Buffer[1]).toHex()); the value in line edit is 0a17.
    In addition i have to do some calculations on the data 03ff and finally display a signed integer value in line edit. please help.

  • Hello again!
    I assume:

    Buffer[1] = 0x03;
    Buffer[2] = 0xff;

    then you can get the interger like this

    #include <QtEndian>
    qint16 integer= qFromBigEndian<qint16>(Buffer.constData() + 1);

    then you can convert it to text


    If you want to use toHex() you should use like this

    int integer = Buffer.mid(1, 2).toHex().toInt(nullptr, 16);

    Buffer.mid(1, 2) means to get a new byte array from index 1, and for length = 2
    toInt(nullptr, 16) means convert a string to int using base 16, aka, hex.
    But this seems a little unnecessary, it is basiclly convert number to string and then convert back to number again.

  • @Bonnie said in Displaying integer value:

    it worked perfectly. thank u for the support

Log in to reply