Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将十六进制转换为带符号的十进制_C++_Qt_Number Formatting_Qtcore - Fatal编程技术网

C++ 将十六进制转换为带符号的十进制

C++ 将十六进制转换为带符号的十进制,c++,qt,number-formatting,qtcore,C++,Qt,Number Formatting,Qtcore,我在将十六进制值转换为带符号的Dec值时遇到问题。我正在使用Qt,这是示例代码 #include <QCoreApplication> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); int x=0xA92B; qDebug()<<x; return a.exec(); } #包括 #包括 int main

我在将十六进制值转换为带符号的Dec值时遇到问题。我正在使用Qt,这是示例代码

#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int x=0xA92B;
    qDebug()<<x;
    return a.exec();
}
#包括
#包括
int main(int argc,char*argv[])
{
qcorea应用程序(argc、argv);
INTX=0xA92B;

qDebug()试试
short x=0xA92B;
,因为如果你使用
int
它会将
0xA92B
存储为一个无符号数字。

我不确定你为什么需要这个,但在结果下面可以看到不同的Qt'ish方法和“short”。不幸的是,我认为你需要short

main.cpp 输出
事实上,这是一个非常好的主意,尽管请注意short可以与整数一样大。由于C++11,并且如果系统支持它,可以使用int16_t代替short。
#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