# Convert from int to hex

• Hello guys i have a problem in converting from int to hex. For example ..
I have the number 203888026 which conversion is 0C27159A. I want this 0 in front of the conversion because i have to do a comparison from a value a take from serial port.
Thanks.
My code is
@uint decimal = 203888026;

The result is
hexadecimal = C27159A without the zero.
how can i make the conversion with the 0 ?

• why do you want to compare the two strings instead of two numbers?

• Try with QByteArray::fromHex(). As far as I know, it prepends that 0.
@
@

Or add 0 yourself :) It needs to be added there only when number of digits is uneven.

• There is a reason but i can't starting explaining the whole story.. but i want to compare two hex string.. that's it.

• As i understand you have hext string, and you want compare it with some number. Thats right? In this case better convert string to number and than compare two numbes. But if you realy want convert decimal to string you can add leading zero manualy.
@uint decimal = 203888026;

• Add if you want to compare a hex-string with the hex-string you received from serial port, you had better convert both to lower case or upper case.

• I stumbled across this thread and was very disappointed that no one actually answered the OP's question, unless you count discrediting requirements as an answer. :-)

For anyone else looking for an answer, QString::arg is exactly what you are after for this sort of conversion, because it has a "base", "fieldwidth" and a "fillchar" field:

QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const

example:
@uint decimal = 203888026;
QString hexvalue = QString("%1").arg(decimal, 8, 16, QLatin1Char( '0' ));@

you can even add a preceding "0x" if you desire, like this:

@QString hexvalue = QString("0x%1").arg(decimal, 8, 16, QLatin1Char( '0' ));@

• Hi mp035,

Welcome to the Qt Dev Net, and thank you for posting a solution!

• This post is deleted!

• @mp035