Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string to long long

I'm using VS 2008 to create a C++ DLL (not managed) project and I need convert a char* to a long long type. Is there an easy way to do it?

Thanks in advance :)

like image 586
Alonso Avatar asked Nov 19 '08 23:11

Alonso


People also ask

Can I convert string to long?

We can convert String to long in java using Long. parseLong() method.

Can we convert string into long in java?

There are many methods for converting a String to a Long data type in Java which are as follows: Using the parseLong() method of the Long class. Using valueOf() method of long class. Using constructor of Long class.

What does Stoll do in C++?

The strtoll() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as a long long int. This function also sets a pointer to point to the first character after the last valid character of the string if there is any, otherwise the pointer is set to null.

How will you convert a string to a long in Python?

By converting a string into long we are translating the value of string type to long type. In Python3 int is upgraded to long by default which means that all the integers are long in Python3. So we can use int() to convert a string to long in Python.


2 Answers

The easiest way is to use the std::stringstream (it's also the most typesafe...)

std::stringstream sstr(mystr);
__int64 val;
sstr >> val;

You may need to target a 64-bit application for this to work.

C++ FAQ

like image 51
Alan Avatar answered Sep 19 '22 10:09

Alan


If you're using boost, lexical_cast is the way to go, in my opinion.

long long ll = boost::lexical_cast<long long>(mystr)
like image 35
Ryan Ginstrom Avatar answered Sep 19 '22 10:09

Ryan Ginstrom