Results 1 to 5 of 5

Thread: converting string to unsigned integer

  1. #1
    Join Date
    Apr 2006
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default converting string to unsigned integer

    Hi,
    Does anybody know how to convert "-2" string to integer?
    I have tried doing it as follows but no success....
    Is there anything wrong with the following code???

    QString s = "-2";

    bool ok;

    uint m = s.toUInt( &ok, 10 );

    Thank you for any help...

  2. #2
    Join Date
    Apr 2006
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: converting string to unsigned integer

    We figured out how to do it....Here it goes....

    QByteArray bArray = s.toLatin1();

    const char * ch = bArray.constData();

    int m = atoi(ch);

    If there is anyother way your welcome to let me know...

    Thanks....


    Quote Originally Posted by mgurbuz
    Hi,
    Does anybody know how to convert "-2" string to integer?
    I have tried doing it as follows but no success....
    Is there anything wrong with the following code???

    QString s = "-2";

    bool ok;

    uint m = s.toUInt( &ok, 10 );

    Thank you for any help...

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: converting string to unsigned integer

    Quote Originally Posted by mgurbuz
    uint m = s.toUInt( &ok, 10 );
    How about asking for and storing the result in a signed integer aswell?
    Qt Code:
    1. int m = s.toInt( &ok, 10 );
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: converting string to unsigned integer

    Tell me, how would you like to store a negative number as an unsigned integer? As 2^32 - 2? Or maybe 2^31? AFAIK -2 is a signed integer...

  5. #5
    Join Date
    Apr 2006
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: converting string to unsigned integer

    Allright I got it....

    I was tried and didn't really read what I wrote....
    and tried many things at that time from toUInt ... to... toInt
    couldn't make it work.
    My question was how to convert a string to signed integer
    and as an expert you understand it perfectly....

    Quote Originally Posted by wysota
    Tell me, how would you like to store a negative number as an unsigned integer? As 2^32 - 2? Or maybe 2^31? AFAIK -2 is a signed integer...

Similar Threads

  1. converting of string to enum
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2008, 17:31
  2. Converting to integer
    By Salazaar in forum Newbie
    Replies: 2
    Last Post: 15th June 2007, 21:11
  3. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 21:49
  4. Converting number to string.
    By safknw in forum Newbie
    Replies: 2
    Last Post: 18th September 2006, 13:12

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.