# Thread: convert Qstring into int

1. Novice
Join Date
Apr 2011
Posts
27
Qt products
Platforms

## convert Qstring into int

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();

2. Beginner
Join Date
Apr 2011
Posts
5

## Re: convert Qstring into int

Hi sabbu,

You can try it :

Qt Code:
`bool convertOK;unsigned long int n = ui->lineEdit()->text().toULong(&convertOK);if(!convertOK) return -1;`
To copy to clipboard, switch view to plain text mode

3. Guru
Join Date
Jan 2006
Location
Germany
Posts
4,380
Thanks
19
Thanked 1,005 Times in 913 Posts
Qt products
Platforms
Wiki edits
5

## Re: convert Qstring into int

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.

4. Intermediate user
Join Date
Mar 2011
Posts
118
Thanked 1 Time in 1 Post
Qt products
Platforms

## Re: convert Qstring into int

You can try this -
Qt Code:
`bool ok;QString str = "9990540087";long no = sss.toLong(&ok,10);`
To copy to clipboard, switch view to plain text mode

5. Guru
Join Date
Jan 2006
Location
Germany
Posts
4,380
Thanks
19
Thanked 1,005 Times in 913 Posts
Qt products
Platforms
Wiki edits
5

## Re: convert Qstring into int

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!

6. Intermediate user
Join Date
Mar 2011
Posts
118
Thanked 1 Time in 1 Post
Qt products
Platforms

## Re: convert Qstring into int

yes i am using 64 bit system
Qt Code:
`bool ok;QString str = "9990540087";quint64 no = str.toLong(&ok,10);`
To copy to clipboard, switch view to plain text mode

it is working fine.

7. Novice
Join Date
Apr 2011
Posts
27
Qt products
Platforms

## Re: convert Qstring into int

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);

8. ## Re: convert Qstring into int

Use this, you need 64 bit number, with 32 bit unsigned you get a max of 4294967295, but you use number larger than this.

Qt Code:
`QString str("9990540087");quint64 number = str.toULongLong();orqulonglong number = str.toULongLong();//or even str.toLongLong() signed version will also work for a typical 10-digit number`
To copy to clipboard, switch view to plain text mode
Last edited by Santosh Reddy; 8th June 2011 at 08:16.

9. ## The following user says thank you to Santosh Reddy for this useful post:

solook (17th September 2012)

10. Novice
Join Date
Apr 2011
Posts
27
Qt products
Platforms

## Re: convert Qstring into int

Thanks ,
this code is work for me.