c++ long long int is not enough?? errors
I am working in c++. I have a string that contains the following number
std::string s= "8133522648";
I want to convert this number in
long long int nr;
I did: nr=atoll(s.c_str()).
The result is: -456410944
. How to solve this error? Thanks
Edit:
In fact I have:
const char* str="81开发者_如何转开发33523648";
I have to convert it into long long int nr=8133523648
Thanks for help! Appreciate!
use int64_t instead of long long. which is defined in stdint.h
If you rely on boost you can use
std::string s= "8133522648";
int64_t nr = boost::lexical_cast<int64_t, std::string>(s);
It can be done in better way as follows:
#include <sstream>
stringstream sstr;
sstr << "8133522648";
long long nr;
sstr >> nr;
Don't use atoll()
as it is not defined by C++ standard. Some compiler may implement it while others don't. Also,
std::string s = 8133522648;
doesn't mean
std::string s = "8133522648";
which was probably what you wanted.
Below code is working fine:
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main() {
std::string s= "8133522648";
long long int nr = atoll(s.c_str());
cout << nr;
}
精彩评论