hexadecimal to decimal conversion
-
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
-
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. -
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
@reshu
first of, a uint32 is a uint32 there is nohexadecimal number
or decimal number inside a QByteArry everything is a char.A hexadecimal number is just a visual representation
in your case here a simpleQByteArray 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);
-
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.