Question

I have a problem with converting Hex values to signed Dec values. I am Using Qt and this is the sample code.

#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int x=0xA92B;
    qDebug()<<x;
    return a.exec();
}

Now I get 43307 but I want to get -22229. Is there a way to do it?

Was it helpful?

Solution

Try short x = 0xA92B; because if you use int it stores 0xA92B as an unsigned number.

OTHER TIPS

I am not sure why you need this, but see the different Qt'ish ways and "short" below what they result. I think you will need short, unfortunately.

main.cpp

#include <QString>
#include <QTextStream>
#include <QDebug>

int main()
{
    int x = 0xA92B;
    short shortX = 0xA92B;
    QString hexString = QString::number(0xA92B);
    QTextStream decTextStream(&hexString);
    int d;
    decTextStream >> d;

    qDebug() << shortX;
    qDebug() << hexString.toInt();
    qDebug() << d;

    return 0;
} 

Building (something similar)

g++ -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core main.cpp && ./a.out

Output

-22229
43307
43307
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top