Hi Martin. Read up on NSString. It's an object for handling Unicode strings, and it's quite different from a C-string, though it includes methods for converting to and from C-strings.
- (IBAction)test1:(id)sender {
NSString *field = @"kabc"; // '@' operator changes C-string to NSString constant
int length = [field length]; // obtain length of NSString
int i = (length <= 5 ? 0 : length - 5); // don't allow index to go below zero
for (; i < length; ++i) {
int c = [field characterAtIndex:i]; // type int; for your purposes int is ok for unichar
//
// not sure what the next 2 lines are for, they're not relevant to your question anyway
// int j = (int) c;
// fAscii = fAscii + j;
//
NSLog(@"char %d = %d", i, c);
}
}
If the input string will be typed by international users (whether or not you localize your app), remember that the int values obtained from the string won't be restricted to the ASCII range. These ints can range from 0 to 65535, so you'll want to decide what to do with ints > 127. If you're doing something like a checksum you might just want c=c%127; or whatever.