March 6, 2012
Qt : conversion in binary, decimal and hex
Qt : conversion in binary, decimal and hex
This is a basic article for Qt : conversion in binary, decimal and hex. From this tutorial, you can know how Qt : conversion in binary, decimal and hex. Qt have rich class to convert from a data from one data type to other. From this tutorial, I am use QString class to convert a number to binary, decimal and hex number.
Please check this main.cpp code :
#include <QCoreApplication> #include <QDebug> #include <QString> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); bool ok; QString str = "FF"; qDebug()<<"Input (HEX) = " << str; int iVal = str.toInt(&ok,16); QString binnumber = str.setNum(iVal, 2); qDebug()<<"Convert to Int = " << QString::number(iVal); qDebug()<<"Convert to Binary = " << binnumber; return (0); }
This is contents for the *.pro file :
TEMPLATE = app TARGET = qt_listdir QT += core HEADERS += SOURCES += main.cpp FORMS += RESOURCES +=
This is an output from this Qt : conversion in binary, decimal and hex code :
toto@toto-laptop:/data/Project/qtproject$ ./convert_data Input (HEX) = "FF" Convert to Int = "255" Convert to Binary = "11111111"
6 Comments
Thank you very much for this tutorial.
There are some issue in this.
If you will convert hex 0x7FFFFFFF to int, binary it retruns 0.
why?
Thank you very much for this tutorial.
There are some issue in this.
1) If you will convert hex 0x7FFFFFFF to int, binary it retruns 0 only.
why?
if you will convert hex 0x7FFFFFFF to int, binary it retruns 0 only.
why?
What is this, my comment automatically edited. I write something else but comment post is skips some of the lines…
Try to use 7FFFFFFF not 0x7FFFFFFF .
Please check the sample main.cpp code.
For anyone who hopes to find valuable information on that topic, right here is the perfect blog I would highly recommend. Feel free to visit my site UQ6 for additional resources about Airport Transfer.