真正长整数字符串到int的转换
Really long integer string to int conversion
我从 Google 驱动器 API 中获取此值作为驱动器大小。
16106127360
如何在 C++ Builder 中将该字符串转换为 int/long/unsigned。
- StrToInt() 不能,它说无效整数。
- atol() 也失败了,returns 乱码
- 环礁岛()?我似乎无法在 C++ Builder
中使用该函数
另外,C++ 构建器的什么数字数据类型可以容纳值 16106127360?
谢谢
此代码使用字符串流,应该可以工作:
#include <sstream>
#include <iostream>
#include <string>
int main()
{
std::string ss = "16106127360";
std::stringstream in;
in << ss;
long long num;
in >> num;
}
16106127360
太大,无法放入 32 位 (unsigned
) int
1。该值需要 64 位 (unsigned
) __int64
或 (unsigned
) long long
。
有很多不同的方法可以将这样的字符串值转换为 64 位整数变量:
在 SysUtils.hpp
header 中有 StrToInt64()
、StrToInt64Def()
和 TryStrToInt64()
函数用于 __int64
值:
__int64 size = StrToInt64("16106127360");
__int64 size = StrToInt64Def("16106127360", -1);
__int64 size;
if (TryStrToInt64("16106127360", size)) ...
(在现代 C++Builder 版本中,unsigned __int64
值也有相应的 UInt64
函数)
stdlib.h
中有strtoll()
/wcstoll()
个函数 header:
long long size = strtoll("16106127360");
long long size = wcstoll(L"16106127360");
stdio.h
header中有sscanf
个函数。使用 %lld
或 %llu
格式说明符:
long long size;
sscanf("16106127360", "%lld", &size);
unsigned long long size;
sscanf("16106127360", "%llu", &size);
long long size;
swscanf(L"16106127360", L"%lld", &size);
unsigned long long size;
swscanf(L"16106127360", L"%llu", &size);
你可以在sstream
中使用std::istringstream
或std::wistringstream
header:
std::istringstream iis("16106127360");
__int64 size; // or unsigned
iis >> size;
std::wistringstream iis(L"16106127360");
__int64 size; // or unsigned
iis >> size;
1:(如果您正在为 iOS 9 编译 C++Builder 项目,long
是 64 位,否则它是 32 位其他支持的平台)
我从 Google 驱动器 API 中获取此值作为驱动器大小。
16106127360
如何在 C++ Builder 中将该字符串转换为 int/long/unsigned。
- StrToInt() 不能,它说无效整数。
- atol() 也失败了,returns 乱码
- 环礁岛()?我似乎无法在 C++ Builder 中使用该函数
另外,C++ 构建器的什么数字数据类型可以容纳值 16106127360?
谢谢
此代码使用字符串流,应该可以工作:
#include <sstream>
#include <iostream>
#include <string>
int main()
{
std::string ss = "16106127360";
std::stringstream in;
in << ss;
long long num;
in >> num;
}
16106127360
太大,无法放入 32 位 (unsigned
) int
1。该值需要 64 位 (unsigned
) __int64
或 (unsigned
) long long
。
有很多不同的方法可以将这样的字符串值转换为 64 位整数变量:
在
SysUtils.hpp
header 中有StrToInt64()
、StrToInt64Def()
和TryStrToInt64()
函数用于__int64
值:__int64 size = StrToInt64("16106127360");
__int64 size = StrToInt64Def("16106127360", -1);
__int64 size; if (TryStrToInt64("16106127360", size)) ...
(在现代 C++Builder 版本中,
unsigned __int64
值也有相应的UInt64
函数)stdlib.h
中有strtoll()
/wcstoll()
个函数 header:long long size = strtoll("16106127360");
long long size = wcstoll(L"16106127360");
stdio.h
header中有sscanf
个函数。使用%lld
或%llu
格式说明符:long long size; sscanf("16106127360", "%lld", &size);
unsigned long long size; sscanf("16106127360", "%llu", &size);
long long size; swscanf(L"16106127360", L"%lld", &size);
unsigned long long size; swscanf(L"16106127360", L"%llu", &size);
你可以在
sstream
中使用std::istringstream
或std::wistringstream
header:std::istringstream iis("16106127360"); __int64 size; // or unsigned iis >> size;
std::wistringstream iis(L"16106127360"); __int64 size; // or unsigned iis >> size;
1:(如果您正在为 iOS 9 编译 C++Builder 项目,long
是 64 位,否则它是 32 位其他支持的平台)