PDA

View Full Version : Convert int to Hex with fixed bytes



metRo_
7th September 2011, 16:04
Hi,
i need to convert an int to hex and after to string but i need to do it with a fixed number of bytes per example:

int a=12;
qDebug()<<QString::number(tam, 16);
=="C" (it is ok)

but i need that the string always have for chars like= "000C"

There is an easy solution for it?

wysota
7th September 2011, 16:10
Yes, read the whole documentation for the function you are using, you can pass more than two parameters to it :)

metRo_
7th September 2011, 16:16
Yes, read the whole documentation for the function you are using, you can pass more than two parameters to it :)

i tried this: qDebug()<<QString::number(tam, 16, 4); and result is:
"12.0000" :s

EDIT1
I'm seeing this: QString QString::arg ( ulong a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const

EDIT2
qDebug()<<QString("%1").arg(tam,4,16,QLatin1Char('0'));

i don't know if this is best way but works :)

"000c"

wysota
7th September 2011, 16:31
It's ok. You can also do:

QString c = QString::number(tam, 16);
if(c.size()<4) c = QString(4-c.size(), '0')+c;