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
Forum Updated to NodeBB v4.3 + New Features

Displaying integer value

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.2k 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 2 May 2020, 07:01 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 2 May 2020, 08:04 last edited by Bonnie 5 Feb 2020, 10:29
      #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 2 May 2020, 11:06 last edited by
        #3

        @Bonnie said in Displaying integer value:

        @Bonnie
        it worked perfectly. thank u for the support

        1 Reply Last reply
        0

        1/3

        2 May 2020, 07:01

        • Login

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