iOS developer
iOS developer

Reputation: 110

How to get ASCII value of a string?

How can I get the ascii value of the string which I enter in the text field? I also want to check if the alphabet which I type is lower case, upper case, comma or space. The function should return YES or NO.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

//    if (string.) {
//        <#statements#>
//    }

        [self performSelector:@selector(delayCall) withObject:nil afterDelay:0.1];


    return YES;

}

Upvotes: 0

Views: 1583

Answers (3)

Arpit
Arpit

Reputation: 12797

first extract the character from string and then assign it to an integer variable. Now this var will contain the ascii value of the input char.

to check for its type check the ascii range let x be the variable containing the ascii value then

if( x>64 && x<91) then print "Capital char" else if(x>96 and x<123) then print "Small char" and so on for comma x==44 for space x==32

to get char from string char c=String(index_pos);

Upvotes: 1

junaidsidhu
junaidsidhu

Reputation: 3580

This is going to help you.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{


    char cStr = [string characterAtIndex:0];

    NSLog(@"%d",(int)cStr);


    return YES;

}  

Upvotes: 2

Nimit Parekh
Nimit Parekh

Reputation: 16864

//char to int ASCII-code
char c = 'b';
int asCode = (int)c;

//int to char
int i = 66; // B
c = (char)i;

Upvotes: 1

Related Questions