unsigned long long from NSString

Discussion in 'iOS Development' started by Steaps, Oct 21, 2009.

  1. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    I am trying to get the unnsigned long long value from an NSString.
    The value is say, "5027706784".

    I have tried this (Which is from a cocoabuilder thread):
    Code:
    @implementation NSString (UnsignedLongLongValue)
    - (unsigned long long)unsignedLongLongValue { 
    	return strtoull([self UTF8String], NULL, 0); 
    }
    @end
    
    With no luck, as it outputs the wrong value:
    Code:
    ReplyID (@): 5027706784 // The string's value.
    ReplyID (#): 732739488 // The value after conversion.
    
    Any help?
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Just take a characters and make a number out of them.

    Or, if you insist, add and implement a category to NSString that adds an - unsignedLongLongValue method. In that method, just take the characters and make a number out of them.

    This is part of the reason it is strongly recommended that people learn C before Objective-C. Issues like this aren't a problem in C. Or C++, really.
  3. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    That code works. Check your format specifier. (For unsigned long long, use %llu).
  4. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Yeah I got that. And I had to change it to base 10.
    Thanks

    Please Register or Log in to view images

    !

Share This Page