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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:22:30+00:00 2026-05-11T03:22:30+00:00

I have a string of digits, e.g. 123456789, and I need to extract each

  • 0

I have a string of digits, e.g. ‘123456789’, and I need to extract each one of them to use them in a calculation. I can of course access each char by index, but how do I convert it into an int?

I’ve looked into atoi(), but it takes a string as argument. Hence I must convert each char into a string and then call atoi on it. Is there a better way?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T03:22:31+00:00Added an answer on May 11, 2026 at 3:22 am

    You can utilize the fact that the character encodings for digits are all in order from 48 (for ‘0’) to 57 (for ‘9’). This holds true for ASCII, UTF-x and practically all other encodings (see comments below for more on this).

    Therefore the integer value for any digit is the digit minus ‘0’ (or 48).

    char c = '1'; int i = c - '0'; // i is now equal to 1, not '1' 

    is synonymous to

    char c = '1'; int i = c - 48; // i is now equal to 1, not '1' 

    However I find the first c - '0' far more readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java String object. I need to extract only digits from it.
I have a string which contains binary digits. How to separate string after each
Using String.Format how can i ensure all numbers have commas after every 3 digits
If i have a string like this: asdf5493 I need the last four digits
i have this string: var s = 'http://xxxxxxx.xxx/abcd123456789?abc=1'; how do i get digits 123456789
How can I get decimal from string hexdecimal: I have unsigned char* hexBuffer =
Can anybody help me with a regex? I have a string with digits like:
I have a string of digits that could vary in length and I want
I have 3 String arrays with constants. eg: String[] digit = {one, two, three};
I have string as abvd.qweqw.sdfs.a=aqwrwewrwerrew . I need to parse this string and get

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.