View Full Version : Conversion from QString to quint32
sksingh73
2nd July 2010, 07:09
I am taking an input from line edit box which is QString by default. I further want to convert this value from QString to quint32. Any suggestions on how to implement this.
QbelcorT
2nd July 2010, 07:39
Hi
If it's a QString then you can use the built in function QString::toUInt(). Read the Qt Assistant docs.
sksingh73
2nd July 2010, 13:42
I want to convert from QString to quint32 & not unsigned int. I have checked Qt Assistant docs, but was not able to find anything there.
Zlatomir
2nd July 2010, 13:52
http://doc.trolltech.com/4.2/qtglobal.html#quint32-typedef
sksingh73
2nd July 2010, 14:41
My code is below, but its not working. Can you suggest corrections in it. Thanx in advance.
QString phrase, bobkey, bobid;
phrase = stringlineEdit->text();
bobkey = keylineEdit->text();
bob_initialkey = (quint32)bobkey.toInt();
bob_userid = (quint32)useridlineEdit->text().toInt();
resulttextEdit->append( "BOB Initial Key: " + display_uint(bob_initialkey) ); //Here output is 0.
resulttextEdit->append( "BOB user ID: " + display_uint(bob_userid) ); //Here output is 0.
qDebug() << "BOB Initial Key: " << bobkey; //This is showing correct value
qDebug() << "BOB Initial Key: " << bob_initialkey; //Here output is 0.
Zlatomir
2nd July 2010, 15:04
Do like this:
QString testInt = "4294967295"; /*make sure that in QString you don't have a big number, that will not fit in unsigned int32*/
quint32 test = testInt.toUInt();
I guess that you don't take the values from that lineEdit, and the string remains empty.
You can use signal and slots to connect the text changed in QLineEdit to a slot (defined by you) witch will initialize the string variables.
If you can't get it to work post the OS, Qt version... and a sample compilable example that replicate the issue.
sksingh73
2nd July 2010, 15:20
its working fine when i am giving input as decimal value. but when i enter hexadecimal value, it displays value as 0.
Zlatomir
2nd July 2010, 15:25
QString testInt = "4294967295"; //biggest value
quint32 test = testInt.toUInt();
str = "FF";
bool ok;
quint32 hex = str.toUInt(&ok, 16); // hex
quint32 dec = str.toUInt(&ok, 10); // dec
qDebug() << test;
qDebug() << hex;
qDebug() << dec;
sksingh73
2nd July 2010, 15:54
Thanks buddy.
Zlatomir
2nd July 2010, 16:01
Just a little note:
bool ok; // you can check the bool variable to see if the conversion worked
quint32 hex = str.toUInt(&ok, 16); // hex
//here the ok will be true, and quint32 hex will be 255
quint32 dec = str.toUInt(&ok, 10); // dec
//here the bool variable will become false because the FF isn't an integer, and quint32 dec will be 0;
Or you can use string that begins with "0x" and base 16 will be used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.