C++:将 std::string 转换为 UINT64
C++: Convert std::string to UINT64
我需要将从文本文件输入的数字的(十进制,如果重要的话)字符串表示形式转换为 UINT64 以传递给我的数据对象。
size_t startpos = num.find_first_not_of(" ");
size_t endpos = num.find_last_not_of(" ");
num = num.substr(startpos, endpos-startpos+1);
UINT64 input;
//convert num to input required here
有什么方法可以像 atoi() 一样将 std::string 转换为 UINT64 吗?
谢谢!
编辑:
下面的工作代码。
size_t startpos = num.find_first_not_of(" ");
size_t endpos = num.find_last_not_of(" ");
num = num.substr(startpos, endpos-startpos+1);
UINT64 input; //= std::strtoull(num.cstr(), NULL, 0);
std::istringstream stream (num);
stream >> input;
使用strtoull or _strtoui64()。
示例:
std::string s = "1123.45";
__int64 n = std::strtoull(s.c_str(),NULL,0);
至少有两种方法可以做到这一点:
构造一个std::istringstream
,并使用我们的老朋友,>>
运算符。
自己转换吧。解析所有数字,一次一个,将它们转换为单个整数。这是一个很好的练习。因为这是一个无符号值,所以甚至不用担心负数。我认为这将是任何介绍性计算机科学中的标准家庭作业 class。至少在我那个时代是这样。
您可以使用 stoull:
char s[25] = "12345678901234567890"; // or: string s = "12345678901234567890";
uint64_t a = stoull(s);
我需要将从文本文件输入的数字的(十进制,如果重要的话)字符串表示形式转换为 UINT64 以传递给我的数据对象。
size_t startpos = num.find_first_not_of(" ");
size_t endpos = num.find_last_not_of(" ");
num = num.substr(startpos, endpos-startpos+1);
UINT64 input;
//convert num to input required here
有什么方法可以像 atoi() 一样将 std::string 转换为 UINT64 吗?
谢谢!
编辑: 下面的工作代码。
size_t startpos = num.find_first_not_of(" ");
size_t endpos = num.find_last_not_of(" ");
num = num.substr(startpos, endpos-startpos+1);
UINT64 input; //= std::strtoull(num.cstr(), NULL, 0);
std::istringstream stream (num);
stream >> input;
使用strtoull or _strtoui64()。 示例:
std::string s = "1123.45";
__int64 n = std::strtoull(s.c_str(),NULL,0);
至少有两种方法可以做到这一点:
构造一个
std::istringstream
,并使用我们的老朋友,>>
运算符。自己转换吧。解析所有数字,一次一个,将它们转换为单个整数。这是一个很好的练习。因为这是一个无符号值,所以甚至不用担心负数。我认为这将是任何介绍性计算机科学中的标准家庭作业 class。至少在我那个时代是这样。
您可以使用 stoull:
char s[25] = "12345678901234567890"; // or: string s = "12345678901234567890";
uint64_t a = stoull(s);