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. Displaying integer value
Qt 6.11 is out! See what's new in the release blog

Displaying integer value

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.8k 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.
  • R Offline
    R Offline
    reshu
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      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

      ui->value->setText(QString::number(integer));
      

      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.

      1 Reply Last reply
      3
      • R Offline
        R Offline
        reshu
        wrote on last edited by
        #3

        @Bonnie said in Displaying integer value:

        @Bonnie
        it worked perfectly. thank u for the support

        1 Reply Last reply
        0

        • Login

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