c++的long long int不够吗??错误
c++ long long int is not enough?? errors
我在c++工作。我有一个包含以下数字
的字符串std::string s= "8133522648";
我想把这个数字转换成
long long int nr;
I did: nr=atoll(s.c_str()).
The result is: -456410944
。如何解决这个错误?由于
事实上我有:
const char* str="8133523648";
I have to convert it into long long int nr=8133523648
谢谢你的帮助!欣赏!
使用int64_t代替long long。它在stint .h
中定义如果你依赖于boost,你可以使用
std::string s= "8133522648";
int64_t nr = boost::lexical_cast<int64_t, std::string>(s);
有以下更好的方法:
#include <sstream>
stringstream sstr;
sstr << "8133522648";
long long nr;
sstr >> nr;
不要使用atoll()
,因为它没有在c++标准中定义。一些编译器可能实现它,而另一些则没有。另外,
std::string s = 8133522648;
并不意味着
std::string s = "8133522648";
这可能是你想要的
下面的代码运行正常:
#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;
}
相关文章:
- 错误:隐式转换更改符号:'int'到'unsigned long'
- C ++分割错误,为什么使用"long long"我没有得到答案?
- 错误:'class std::unique_ptr<std::set<long unsigned int> >'没有名为 'size' 的成员
- 错误:调用'begin(long double [nPoints])'没有匹配函数;使用硬编码的 int 与整数变量初始化向量
- 只读位置'__result.std::_Rb_tree_const_iterator<_Tp>::operator*<long long int>()'分配错误
- 错误:将"long int (*)[4]"分配给"long int [4][4]"时的类型不兼容
- vtkTypeTraits.h 错误:"Type long is not 4 or 8 bytes in size."
- 连接到MySQL时如何修复xdev C++ api中的'string too long'错误
- 在字符串的std::映射中找到编译错误,long
- 如何修复错误"long* is incompatible with U32**"?
- 我有 3 个错误:期望一个")",期望一个表达式,long 类型的参数与 U32 类型的参数不兼容
- 当我使用由unsigned int和unsigned long区分的重载函数时,引发了不明确的函数调用错误?这种重载有效
- 错误:从'int (*)()'到'long unsigned int'的转换无效
- 错误:无法将'long unsigned int (A::*)()'转换为'poFunc {aka long unsigned int (*)()}'
- 编译器错误"character constant too long for its type" 。怎么了?
- GCC 4.1.2:错误:整型常量对于' long '类型来说太大
- c++从long转换为float的奇怪错误
- Visual Studio 2008下使用long long int的编译器错误的原因是什么?
- 如何在Windows Server 2012上工作时出现"OverflowError: Python int too large to convert to C long"错误进行转换?
- c++的long long int不够吗??错误