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

hexadecimal to decimal conversion

Scheduled Pinned Locked Moved Unsolved General and Desktop
ui-encodersp
4 Posts 3 Posters 1.7k 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 reshu
    #1

    Hi
    I am trying to convert a 32 bit hexadecimal number located at 4 consecutive locations in a QByte array to decimal number. but i am not getting the correct value.```
    QByteArray D1;
    D1.resize(4);
    D1[0]=0xAB; //MSB
    D1[1]=0xCD;
    D1[2]=0xEF;
    D1[3]=0x00; //LSB

    //// option 1
    encoder_sp_temp = qFromBigEndian<qint32>(D1.mid(0, 4).toHex());
    ui->encoder_sp->setText(QByteArray::number(encoder_sp_temp));
    ui->encoder_sp->setText(QByteArray::number(encoder_sp_temp,10));
    Answer i am getting is 1633837924 instead of 288240000

    //// option 2
    ui->encoder_sp->setText(QByteArray().append(D1[0]).append(D1[1]).append(D1[2]).append(D1[3]).toHex());
    hex value displayed correctly

    ///option 3
    qint32 value=D1.at(0)<<24|D1.at(1)<<16|D1.at(2)<<8|D1.at(0);
    ui->encoder_sp->setText(QString::number(value,10));

    please help to resolve this issue
    Reshu
    J.HilkJ 1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      Why are you using toHex()????That will convert the number to hex string!!! A string!!! Not number!!! Just

      encoder_sp_temp = qFromBigEndian<quint32>(D1.constData());
      

      Do note that encoder_sp_temp should be a quint32 instead of qint32, or you'll get a negative number.

      Since I've answered your several questions, I think you really need to understand toHex() and QByteArray...
      The result of toHex() is a string, you can't use it as a number anymore.

      R 1 Reply Last reply
      3
      • R reshu

        Hi
        I am trying to convert a 32 bit hexadecimal number located at 4 consecutive locations in a QByte array to decimal number. but i am not getting the correct value.```
        QByteArray D1;
        D1.resize(4);
        D1[0]=0xAB; //MSB
        D1[1]=0xCD;
        D1[2]=0xEF;
        D1[3]=0x00; //LSB

        //// option 1
        encoder_sp_temp = qFromBigEndian<qint32>(D1.mid(0, 4).toHex());
        ui->encoder_sp->setText(QByteArray::number(encoder_sp_temp));
        ui->encoder_sp->setText(QByteArray::number(encoder_sp_temp,10));
        Answer i am getting is 1633837924 instead of 288240000

        //// option 2
        ui->encoder_sp->setText(QByteArray().append(D1[0]).append(D1[1]).append(D1[2]).append(D1[3]).toHex());
        hex value displayed correctly

        ///option 3
        qint32 value=D1.at(0)<<24|D1.at(1)<<16|D1.at(2)<<8|D1.at(0);
        ui->encoder_sp->setText(QString::number(value,10));

        please help to resolve this issue
        Reshu
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @reshu
        first of, a uint32 is a uint32 there is no hexadecimal number or decimal number inside a QByteArry everything is a char.

        A hexadecimal number is just a visual representation
        in your case here a simple

        QByteArray D1;
            D1.resize(4);
            D1[0]=0xAB; //MSB
            D1[1]=0xCD;
            D1[2]=0xEF;
            D1[3]=0x00; //LSB
        
        
            quint32 value = qFromBigEndian<quint32>(D1);
            qDebug() << value << QString::number(value, 10) << QString::number(value, 16);
        

        will do just fine.
        But since you're doing mid() calls, I suggest a QDataStream instead:

        QByteArray D1;
            D1.resize(4);
            D1[0]=0xAB; //MSB
            D1[1]=0xCD;
            D1[2]=0xEF;
            D1[3]=0x00; //LSB
            //Junk data
            D1[4]= 0xFF;
            D1[5]= 0xFF;
            D1[6]= 0xFF;
            D1[7]= 0xFF;
            //other number
            D1[8]=0xAB; //MSB
            D1[9]=0xCD;
            D1[10]=0xEF;
            D1[11]=0x00; //LSB
        
        
            QDataStream out(D1);
            out.setByteOrder(QDataStream::BigEndian);
        
            quint32 value1;
            out >> value1;
            out.skipRawData(4);
        
            quint32 value2;
            out >> value2;
            qDebug() << value1 << QString::number(value1, 10) << QString::number(value1, 16);
            qDebug() << value2 << QString::number(value2, 10) << QString::number(value2, 16);
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        5
        • B Bonnie

          Why are you using toHex()????That will convert the number to hex string!!! A string!!! Not number!!! Just

          encoder_sp_temp = qFromBigEndian<quint32>(D1.constData());
          

          Do note that encoder_sp_temp should be a quint32 instead of qint32, or you'll get a negative number.

          Since I've answered your several questions, I think you really need to understand toHex() and QByteArray...
          The result of toHex() is a string, you can't use it as a number anymore.

          R Offline
          R Offline
          reshu
          wrote on last edited by
          #4

          @Bonnie thanks. got correct when changed to quint32. actually the data is that of an encoder which gives both positive and negative values depending on direction of rotation.
          Reshu

          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