I have a very long number with hundreds of digits which I have stored in a string. Now, I want to read it into an integer/long digit-by-digit.
long int strtol ( const char * str, char ** endptr, int base );
takes an end-pointer, but it doesn't seem like I can set it right, because it tries to read in all digits, and hence, returns LONG_MAX
.
char str[]=
"70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586";
char *pEnd; //set pEnd to str[1]'s address how???
long num = strtol(str, &pEnd, 10);
cout << num << endl; //LONG_MAX
I thought of solutions like copying it to a new character array, and then applying strtol
but that doesn't seem like the best way to do it.
So what would be the easiest/best way to do this?