NSString to ASCII

Hi developers,

I need convert NSString to ASCII characters{numbers - etc. k=107}.
can i use this code:
NSString *field = "k";
for (int i = [field length]-5; i < [field length]; ++i) {
char c = [field characterAtIndex:i];
int j = (int) c;
fAscii = fAscii + j;
}

I need number from string, because after convert the string I use this number in mathematical operation.

Thanks for any tips!
Martin

Mac OS X (10.5.6)

Posted on Mar 9, 2009 10:58 AM

Reply
10 replies

Mar 9, 2009 2:31 PM in response to krtek

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.

Mar 9, 2009 2:47 PM in response to jtgrassie

Ooops.. I sure didn't mean to repeat your solution, jtgrassie!! No matter how old a thread may be, I seem to have the amazing ability to cause other developers to respond to it while I'm editing my post. I try to refresh the forum page immediately before clicking Post Message, but I often don't see the prior response until afterwards. I guess it never hurts to have two identical opinions, just I feel like an idiot.

May 29, 2009 4:49 AM in response to krtek

krtek wrote:
I need connecting this four numbers (107,97,98,99) to one number or string.

Please show me exactly what the one resulting number would be, or what the string would look like.

E.g., do you want to form the decimal number 107979899 or the string "107979899" ? Or would you want the number 107097098099 ? Note that not all concatenations will allow you to recover the original numbers, in case that matters. Will you need to encode strings longer than four? Are the chars in the string limited to ASCII? If not, what is the char set?

In any case. I need a precise specification so we don't waste any more time, ok?

May 29, 2009 7:09 AM in response to RayNewbie

Yes, I want to form the decimal number. I am using numbers without 0 at the first place (107979899).
My idea is:
after converts the nsstring to numbers, In the cycle "for" I use add c+c and the last cycle I will have NSString (107979899).
After cycle "for" I will convert Nsstring to number.
I use Nsstring, because I do not know what I use data type that addition in this numbers.
Thanks

May 29, 2009 11:01 PM in response to krtek


- (unsigned long long)encodeString:(NSString*)field {
NSMutableString *digitString = [NSMutableString stringWithCapacity:18];
for (int i = 0; i < [field length]; i++) {
int c = [field characterAtIndex:i];
NSLog(@"char %d = %d", i, c);
[digitString appendFormat:@"%d", c];
}
if ([digitString length] > 18) {
// decimal value will overflow unsigned 64 bit integer
NSLog(@"encodeString: Overflow");
// digitString could be truncated here if desired
return 0;
}
return [digitString longLongValue];
}
- (IBAction)test {
unsigned long long myResult = [self encodeString:@"kabc"];
NSLog(@"myResult=%qu", myResult);
myResult = [self encodeString:@"kabcéák"];
NSLog(@"myResult=%qu", myResult);
}

[Session started at 2009-05-29 22:41:40 -0700.]
2009-05-29 22:43:16.072 TestBed[39806:20b] char 0 = 107
2009-05-29 22:43:16.072 TestBed[39806:20b] char 1 = 97
2009-05-29 22:43:16.072 TestBed[39806:20b] char 2 = 98
2009-05-29 22:43:16.075 TestBed[39806:20b] char 3 = 99
2009-05-29 22:43:16.075 TestBed[39806:20b] myResult=107979899
2009-05-29 22:43:16.075 TestBed[39806:20b] char 0 = 107
2009-05-29 22:43:16.076 TestBed[39806:20b] char 1 = 97
2009-05-29 22:43:16.076 TestBed[39806:20b] char 2 = 98
2009-05-29 22:43:16.076 TestBed[39806:20b] char 3 = 99
2009-05-29 22:43:16.077 TestBed[39806:20b] char 4 = 233
2009-05-29 22:43:16.077 TestBed[39806:20b] char 5 = 225
2009-05-29 22:43:16.077 TestBed[39806:20b] char 6 = 107
2009-05-29 22:43:16.078 TestBed[39806:20b] myResult=107979899233225107

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

NSString to ASCII

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.