Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved hexadecimal to decimal conversion

    General and Desktop
    ui-encodersp
    3
    4
    598
    Loading More Posts
    • 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
      reshu last edited by 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.Hilk 1 Reply Last reply Reply Quote 0
      • B
        Bonnie last edited by 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 1 Reply Last reply Reply Quote 3
        • J.Hilk
          J.Hilk Moderators @reshu last edited by

          @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

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

          1 Reply Last reply Reply Quote 5
          • R
            reshu @Bonnie last edited by

            @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 Reply Quote 0
            • First post
              Last post