Results 1 to 9 of 9

Thread: convert Qstring into int

  1. #1
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Post 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();
    we r find o, please help me how to convert this type of string in integer.

  2. #2

    Default Re: convert Qstring into int

    Hi sabbu,

    You can try it :

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


  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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. #4
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: convert Qstring into int

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

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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. #6
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: convert Qstring into int

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

    it is working fine.

  7. #7
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default 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. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default 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:
    1. QString str("9990540087");
    2. quint64 number = str.toULongLong();
    3. or
    4. qulonglong number = str.toULongLong();
    5. //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. #9
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default Re: convert Qstring into int

    Thanks ,
    this code is work for me.

Similar Threads

  1. How to convert from QString to string ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 1st December 2010, 02:50
  2. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 10:10
  3. how to convert int to QString?
    By phillip_Qt in forum Newbie
    Replies: 2
    Last Post: 5th October 2007, 09:07
  4. How to convert Int to QString in QT4?
    By drake1983 in forum Newbie
    Replies: 2
    Last Post: 11th March 2007, 07:58
  5. How to convert from QString to quint16 ?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 31st March 2006, 10:00

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.