PDA

View Full Version : convert Qstring into int



sabbu
6th June 2011, 15:15
Sir ,
i have a Qstring (10 digit number suppose that ex. 9990540087) i want convert in integer but we find zero .
source code:
Qstring str ;
str=ui->lineEdit->text();
int n;
n=str.toInt();
we r find o, please help me how to convert this type of string in integer.

didji31
6th June 2011, 15:39
Hi sabbu,

You can try it :



bool convertOK;
unsigned long int n = ui->lineEdit()->text().toULong(&convertOK);
if(!convertOK) return -1;

:)

Lykurg
6th June 2011, 15:39
A 32bit (which is probably what you use) unsigned int can only store values till 4.294.967.295. So you should use a long or something that can handle such large numbers.

bibhukalyana
6th June 2011, 15:45
You can try this -


bool ok;
QString str = "9990540087";
long no = sss.toLong(&ok,10);

Lykurg
6th June 2011, 15:46
let me guess, you are working on a 64bit system. To ensure, that your code is working everywhere one probably should use q(u)int64.

EDIT: Eh, you should stay with the thread starters number! 1234567809 != 9990540087 and 1234567809 is small enough to get into a 32 bit signed integer!

bibhukalyana
6th June 2011, 15:52
yes i am using 64 bit system


bool ok;
QString str = "9990540087";
quint64 no = str.toLong(&ok,10);


it is working fine.

sabbu
8th June 2011, 07:59
sir,
i could not success i am tired.My operting system is 32 bit. when i convert a string 10 digit number find only 0 .if i convert string 9 digit number i find 9 digit .i use all below code but i am not success.please help me
1.bool ok;
2.QString str = "9990540087";
3.quint64 no = str.toLong(&ok,10);
///////////////////////////////////
1.bool convertOK;
2.unsigned long int n = ui->lineEdit()->text().toULong(&convertOK);
3.if(!convertOK) return -1;
//////////////
1. bool ok;
2.QString str = "9990540087";
3.long no = sss.toLong(&ok,10);

Santosh Reddy
8th June 2011, 08:06
Use this, you need 64 bit number, with 32 bit unsigned you get a max of 4294967295, but you use number larger than this.


QString str("9990540087");
quint64 number = str.toULongLong();
or
qulonglong number = str.toULongLong();
//or even str.toLongLong() signed version will also work for a typical 10-digit number

sabbu
8th June 2011, 17:31
Thanks ,
this code is work for me.