[Qt-interest] QString (hex) to unsigned int conversion
Sean Harmer
sean.harmer at maps-technology.com
Wed Dec 22 10:50:06 CET 2010
On 22/12/2010 09:34, Carel Combrink wrote:
> Hi,
>
> Is there an easy way to convert a QString containing a hex value to
> integer. See example below:
>
> bool ok;
> ui->lineEdit->setText("0xABCD");
> //ui->lineEdit->setText("0xabcd");
> //ui->lineEdit->setText("43981");
> unsigned int value = ui->lineEdit->text().toUInt(&ok);
Change the above line to:
unsigned int value = ui->lineEdit->text().toUInt(&ok, 16);
if you do not include the "0x" prefix.
If you do include the "0x" prefix then you can use a base of 0:
unsigned int value = ui->lineEdit->text().toUInt(&ok, 0);
From the QString docs:
"If base is 0, the C language convention is used: If the string begins
with "0x", base 16 is used; if the string begins with "0", base 8 is
used; otherwise, base 10 is used."
Sean
More information about the Qt-interest-old
mailing list