是否有一种将字符串转换为无符号整数的方法? _ultoa函数可以将无符号长整型转换为字符串,但找不到相反的版本...
std::strtoul()
是一个选择。还有旧的函数像 atoi()
。
#include <boost/lexical_cast.hpp>
[...]
unsigned int x = boost::lexical_cast<unsigned int>(strVal);
或者,您可以使用stringstream(它基本上是在内部执行了lexical_cast的操作):
#include <sstream>
[...]
std::stringstream s(strVal);
unsigned int x;
s >> x;
sscanf会做你想要的事情。
char* myString = "123"; // Declare a string (c-style)
unsigned int myNumber; // a number, where the answer will go.
sscanf(myString, "%u", &myNumber); // Parse the String into the Number
printf("The number I got was %u\n", myNumber); // Show the number, hopefully 123
如果你通过_atoi64进行转换,它就能正常工作。
unsigned long l = _atoi64(str);
那么 int atoi ( const char * str ) 怎么样?
string s("123");
unsigned u = (unsigned)atoi(s.c_str());