Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you convert a C++ string to an int? [duplicate]

Possible Duplicate:
How to parse a string to an int in C++?

How do you convert a C++ string to an int?

Assume you are expecting the string to have actual numbers in it ("1", "345", "38944", for example).

Also, let's assume you don't have boost, and you really want to do it the C++ way, not the crufty old C way.

like image 366
krupan Avatar asked Oct 14 '08 05:10

krupan


People also ask

Can you convert a string to an int in C?

In C, the atoi() function converts a string to an integer.

What is Strtol () in C?

The strtol library function in C converts a string to a long integer. The function works by ignoring any whitespace at the beginning of the string, converting the next characters into a long integer, and stopping when it comes across the first non-integer character.

What atoi does in C?

The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number.


1 Answers

#include <sstream>  // st is input string int result; stringstream(st) >> result; 
like image 92
Randy Sugianto 'Yuku' Avatar answered Oct 09 '22 03:10

Randy Sugianto 'Yuku'