How to convert a char array into an int in C++?

C++ problem: I have a char's array containing a number that I want to move to an int, how do I do that?

  • example... int a; char b[3]='123'; //(I dunno if this is even possible but the fact is I have a char array containing a many digits number) a=b; how do I do this?

  • Answer:

    Okay, you can't store the sting in a char, but you can store it in an array of char[]; the syntax you are looking for is atoi int i = atoi("123"); #include <stdio.h> #include <stdlib.h> int main () { int i; char szInput [256]; printf ("Enter a number: "); fgets ( szInput, 256, stdin ); i = atoi (szInput); printf ("The value entered is %d. The double is %d.\n",i,i*2); return 0; }

aoc10010... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

1. determine how many digits (if this is not fixed) 2. a=b[last]+b[secondlast]*10.....b[first]*… (this can be done in a loop)

unsigned char tempArr[4]='123'; int i=0, arrToInt=0; for(i=0;i<4;i++) arrToInt =(arrToInt<<8) | tempArr[i];

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.