PDA

View Full Version : Convert string to int iPhone SDK




jmac1074
Apr 27, 2008, 10:31 AM
Does anyone know how you can convert a string to an int in the iPhone SDK?

thanks.



lee1210
Apr 27, 2008, 10:43 AM
From the apple reference documents:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue

intValue is what you're looking for.

-Lee

taberrr
Jul 3, 2009, 03:05 PM
From the apple reference documents:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.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. :(

lee1210
Jul 3, 2009, 04:37 PM
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/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/characterAtIndex:

Which returns a unichar:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/c_ref/unichar

You could get back to an NSString with:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/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

taberrr
Jul 3, 2009, 05:15 PM
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!


unichar specialChar = [myStr characterAtIndex: 0];
specialChar = specialChar + 2;

NSLog(@"string: %@", [NSString stringWithCharacters: &specialChar length:1]);


thanks again!

lee1210
Jul 3, 2009, 05:24 PM
glad you got what you needed... it's no surprise since unichar is just:
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

taberrr
Jul 3, 2009, 05:29 PM
glad you got what you needed... it's no surprise since unichar is just:
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!

lee1210
Jul 3, 2009, 05:37 PM
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

taberrr
Jul 3, 2009, 06:04 PM
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!