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"
5 Comments

Add a Comment

Your email address will not be published. Required fields are marked *