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 8330375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:06:16+00:00 2026-06-09T02:06:16+00:00

The following piece of code is supposed to return two integers: val1 = 2

  • 0

The following piece of code is supposed to return two integers: val1 = 2 and val2 = 5.

    NSString *col = @"1245DD";

    char c1 = [col characterAtIndex:1];
    char c2 = [col characterAtIndex:3];

    int val1 = [[[NSString alloc] initWithUTF8String:&c1] intValue];
    int val2 = [[[NSString alloc] initWithUTF8String:&c2] intValue];

inspecting values at runtime:

  • c1 = ‘2’

  • c2 = ‘5’

good so far.

But then:

  • val1 = 2
  • val2 = 52

I don’t understand why val2 always ends up being the concatenation of c2 and c1. What am I missing?
thanks,

  • 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-09T02:06:18+00:00Added an answer on June 9, 2026 at 2:06 am

    You are creating an NSString object as a UTF-8 string, that is in fact a single character. You need to NUL-terminate the UTF-8 string if you want to use it like this.

    Note that [NSString characterAtIndex:] returns a unichar, not a char, so use [NSString initWithCharacters:length:] instead where you can tell the method how many characters to use:

    NSString col = @"1245DD";
    
    unichar c1 = [col characterAtIndex:1];
    unichar c2 = [col characterAtIndex:3];
    
    int val1 = [[[NSString alloc] initWithCharacters:&c1 length:1] intValue];
    int val2 = [[[NSString alloc] initWithCharacters:&c2 length:1] intValue];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose the following piece of code struct S { S(int & value): value_(value) {}
Is the following piece of code supposed to work? bool b1 = true; bool
I have the following piece of python code. The code is supposed to get
Consider following piece of code: JSONObject json = new JSONObject(); json.put(one, 1); json.put(two, 2);
I have a piece of code which is similar to the following: final int
the following piece of code works fine when i use it among some code
The following piece of code is used to print the time in the logs:
The following piece of code should read each line of the file and operate
The following piece of code runs fine when parallelized to 4-5 threads, but starts
The following piece of code is working on Android devices and iPhone simulator but

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.