Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:14:18+00:00 2026-06-01T14:14:18+00:00

I want to convert characters into integers based on predetermined values, for example: a

  • 0

I want to convert characters into integers based on predetermined values, for example:

a = 0 
b = 1
c = 2
d = 3

etc…

Right now I’m doing it with an If/Else If, I just want to know if there is a faster/better way I should be doing it because the list of conversions may get quite long.

Here’s what I’m using now:

-(NSInteger)ConvertToInt:(NSString *)thestring {
    NSInteger theint;
    if([thestring isEqualToString:@"a"] == YES){
        theint = 0;
    } else if ([thestring isEqualToString:@"b"] == YES){
        theint = 1;
    } //etc...

    return theint;
}

This works fine, but as I said, if it makes more sense can I create an array with all the key/values then just run through that to return the integers?

Please provide examples as I’m a beginner with Objective C/iOS. I come from Web languages.

Thanks!

EDIT: Thanks for the help everyone. I used taskinoors answer but I replaced the NSDictionary which was giving error messages with this:

NSDictionary *dict;
dict = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithInt:0], @"a",
        [NSNumber numberWithInt:1], @"b",
        [NSNumber numberWithInt:2], @"c", nil];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:14:19+00:00Added an answer on June 1, 2026 at 2:14 pm
    unichar ch = [thestring characterAtIndex:0];
    theint = ch - 'a';
    

    Note that, 'a' with a single quote is character a, not string "a".

    If the values are not regular like your example then you can store all predefined values into a dictionary. For example:

    "a" = 5;
    "b" = 1;
    "c" = 102;
    
    NSArray *values = [NSArray arrayWithObjects:[NSNumber numberWithInt:5],
        [NSNumber numberWithInt:1], [NSNumber numberWithInt:102], nil];
    NSArray *keys = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
    NSDictionary *dic = [NSDictionary dictionaryWithObjects:values forKeys:keys];
    
    theint = [[dic valueForKey:thestring] intValue];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert an integer into its character equivalent based on the alphabet.
I have a string $regexp_as_string Now I want to convert it into an regex
I want to convert a four character string (i.e. four characters) into a long
So what I want, is for example to convert the letter 'a' into 97
I want to convert an e-mail address into HTML Escape Characters as a basic
I want to convert a binary file into an array of ascii charcters .
I want to convert String to Date in different formats. For example, I am
I want to convert this linear loop into a concurrent one: for(Item item :
I want to convert a floating point user input into its integer equivalent. I
I want to convert a single character of a string into an integer, add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.