Convert string to int iPhone SDK

Discussion in 'iOS Programming' started by jmac1074, Apr 27, 2008.

  1. macrumors newbie

    Joined:
    Apr 24, 2008
    #1
    Does anyone know how you can convert a string to an int in the iPhone SDK?

    thanks.
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
  3. macrumors regular

    Joined:
    Jun 19, 2007
    Location:
    NY
    #3
    sorry to bring up an old thread, but i'm trying to use this and i was just wondering how you'd go about doing this with a string that contains international characters such as ü, etc.

    (for example, [@"ü" intValue] returns -4) ... but when i then convert this back to a string, -4 doesn't seem to work. :(
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    ü is not a number in any number system I am familiar with. As such, i wouldn't expect a "good" value to be returned from intValue.
    If you're wanting the unicode code point, you might be able to get there via:
    http://developer.apple.com/document...pple_ref/occ/instm/NSString/characterAtIndex:

    Which returns a unichar:
    http://developer.apple.com/document...e/NSString.html#//apple_ref/doc/c_ref/unichar

    You could get back to an NSString with:
    http://developer.apple.com/document...occ/instm/NSString/initWithCharacters:length:

    It takes a unichar array, but it should be trivial to get one that's length one and stick a single unichar in there.

    -Lee
     
  5. macrumors regular

    Joined:
    Jun 19, 2007
    Location:
    NY
    #5
    thanks a ton, lee! i wanted to stick with intValue so that i could modify it like so: (theIntValue + 2) but this seems to work fine!

    Code:
    unichar specialChar = [myStr characterAtIndex: 0];
    specialChar = specialChar + 2;
    
    NSLog(@"string: %@", [NSString stringWithCharacters: &specialChar length:1]);
    
    thanks again!
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    glad you got what you needed... it's no surprise since unichar is just:
    Code:
    typedef unsigned short unichar;
    Which means it's an unsigned 2-byte integer. However, you should be careful with addition on characters. Encodings can bite you here.

    -Lee
     
  7. macrumors regular

    Joined:
    Jun 19, 2007
    Location:
    NY
    #7
    ah yeah, nice. i'm just using it that way purely for visual effect... what's the worst that would happen? if the newly added unichar was outside the code page, the app may crash? i wonder if there's a way to do like... isValidUnichar(unichar + 1) or something. :)

    thanks!
     
  8. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    Certainly nothing terrible will happen. As long as you "don't care" about the character generated (it may be unprintable, etc.), there's no real problem. You're not doing pointer math, so you won't get crashes based on bad pointers. If you overflow the unsigned short, it will just wrap around. Again, if you don't care what character you get, no big deal.

    -Lee
     
  9. macrumors regular

    Joined:
    Jun 19, 2007
    Location:
    NY
    #9
    cool, sounds good. thanks a bunch for the help!
     

Share This Page