tags:

views:

660

answers:

3

I was wondering if anyone has the following php function equivalents in Objective-C for iPhone development:

  1. ord() # returns the ASCII value of the first character of a string.
  2. chr() # returns a character from the specified ASCII value.

Many thanks!

+1  A: 
//char to int ASCII-code
char c = 'a';
int ascii_code = (int)c;

//int to char
int i = 65; // A
c = (char)i;
luvieere
char in c is a "smaller" int so you don't need to cast char to int
Al Bundy
Thanks luvieere for the answer! Much appreciated.
topace
+2  A: 

This is how you can work with ASCII values and NSString. Note that since NSString is working with unichars, there could be unexpected results for a non ASCII string.

// NSString to ASCII
NSString *string = @"A";
int asciiCode = [string characterAtIndex:0]; // 65

// ASCII to NSString
int asciiCode = 65;
NSString *string = [NSString stringWithFormat:@"%c", asciiCode]; // A
alleus
This is working perfectly for me thank you!
topace
A: 

Thanks alleus. It is all I need for my apps.

kch14ng
@kch14ng - welcome to Stack Overflow. I'm sure your sentiments are appreciated, but this is not an answer to the question. Please restrict your posts to useful information.
McDowell
Sorry McDowell, I am new to Stack Overflow.
kch14ng