如何跨平台将char*转换为长(64位)?
-
29-10-2019 - |
题
如何将char*字符串转换为长(64位)整数?
我使用MSVC和GCC编译器,我的平台是Windows,Linux和Mac OS。
谢谢。
解决方案
采用 strtoull
对于无签名长或 strtoll
长期签名。在任何UNIX(Linux,Mac OS X)上,键入 man strtoull
或者 man strtoll
获取其描述。由于两者都是C99标准的一部分,因此应在支持C的任何系统上可用。 有有关如何使用它们的示例.
其他提示
对于C ++,带有支持的编译器 long long int
, ,我会使用 std::istringstream
目的。例如:
char* number_string;
//...code that initializes number_string
std::istringstream input_stream(number_string);
long long int i64_bit_type;
input_stream >> i64_bit_type;
long long int i;
if(sscanf(string, "%lld", &i) == 1) { ... }
boost::lexical_cast
可能是最简单的(在代码中)。看 http://www.boost.org/doc/libs/1_47_0/libs/conversion/lexical_cast.htm 有关更多信息。或者使用 stringstream
解析数值。
#include <stdlib.h>
char serial[1000];
long long var = _atoi64(serial);
不隶属于 StackOverflow