views:

2232

answers:

4

Hi, 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 :)

+2  A: 

Try _atoi64. This takes char* and returns __int64.

altruic
Buyer beware: atoi is not type safe nor does it offer any bounds checking!
Alan
Yeah, _atoi64 is probably not a good choice. If you're using .NET framework, why not use ToInt64 on a String instead of a char*. If not, I think strtol or it's equiv is the current standard way to go and is safer than atoi.
Jason Coco
boost::lexical_Cast<>
Martin York
+2  A: 

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

Alan
+2  A: 

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

long long ll = boost::lexical_cast<long long>(mystr)
Ryan Ginstrom
+1  A: 

Asked and answered soooo many times: See: http://stackoverflow.com/questions/200090/how-do-you-convert-a-c-string-to-an-int

Martin York
Not an exact duplicate IMO. It has the same answer as that other question ("lexical_cast if you have boost, else stringstream"), but this question is about long long, and that question was about int. You could maybe "merge" them by changing that question to ask about all integer types?
Steve Jessop