Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jmac1074

macrumors newbie
Original poster
Apr 24, 2008
6
0
Does anyone know how you can convert a string to an int in the iPhone SDK?

thanks.
 
From the apple reference documents:
http://developer.apple.com/document....html#//apple_ref/occ/instm/NSString/intValue

intValue is what you're looking for.

-Lee

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. :(
 
ü 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
 
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!
 
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
 
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

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!
 
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
 
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

cool, sounds good. thanks a bunch for the help!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.