Reputation: 772
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?
Upvotes: 2
Views: 1402
Reputation: 53225
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.
#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;
}
g++ -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core main.cpp && ./a.out
-22229
43307
43307
Upvotes: 0
Reputation: 754
Try short x = 0xA92B;
because if you use int
it stores 0xA92B
as an unsigned number.
Upvotes: 5