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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#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 :
1 2 3 4 5 6 7 8 9 |
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 :
1 2 3 4 |
toto@toto-laptop:/data/Project/qtproject$ ./convert_data Input (HEX) = "FF" Convert to Int = "255" Convert to Binary = "11111111" |
My name is Toto Sugito. This is my notes when I try something. Maybe, this is NOT THE BEST SOLUTION for your problems. If you have other method or idea that better with my post, please share in this blog. Thank for visiting my site.
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.