C++ 如何将ascii转换为无符号整数

C++ 如何将ascii转换为无符号整数,c++,C++,是否有一种方法可以将字符串转换为无符号整数? _ultoa存在,但找不到vise verse版本…就是这个版本。还有一些老版本,比如Boost提供了词法转换 #include <boost/lexical_cast.hpp> [...] unsigned int x = boost::lexical_cast<unsigned int>(strVal); #包括 [...] 无符号整数x=boost::词法转换(strVal); 或者,您也可以使用stringstre

是否有一种方法可以将字符串转换为无符号整数?
_ultoa存在,但找不到vise verse版本…

就是这个版本。还有一些老版本,比如Boost提供了词法转换

#include <boost/lexical_cast.hpp>
[...]
unsigned int x = boost::lexical_cast<unsigned int>(strVal);
#包括
[...]
无符号整数x=boost::词法转换(strVal);
或者,您也可以使用stringstream(这基本上就是lexical_cast在封面下所做的):

#包括
[...]
std::stringstreams(strVal);
无符号整数x;
s>>x;
怎么样

我会做你想做的

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就行了


无符号长l=_atoi64(str)

如果您想要非十进制解释,请参阅:我是唯一一个喜欢流插入但讨厌流提取的人吗?我每次都会使用函数(比如这个boost函数,不过老实说,我可能仍然会不假思索地使用atoi)。是的,流提取非常难看,特别是因为你不能用它初始化常量。不过它也有相当大的威力,因为你可以使用操纵器来改变你的基础,等等。我恨scanf家族,甚至早在C。它从来没有做过我想让它做的事情,而且大多数时候它导致了一个bug。如果您知道字符串与您的需求相匹配(如果您已经检查了它或使用其他代码提取了它),那么就可以了,但是在这种情况下,为什么不使用atoi或其他什么呢?任何使用scanf家庭功能的人——嗯,有些罪行的惩罚都不会太严厉;-)这应该是“任何人都使用C++中的SCANF家族函数……”Atoi不做无符号,对吧?在Windows CRT中,如果发生溢出,它将返回一个错误(Erage)。Cheeso,是的,我可以通过“I”来判断,它是int;-)这根本不是一个好建议。他特别要求使用“unsigned int”,并提到了类似“uUltoa”的东西。海报了解atoi()、atol()等。这些函数都是有符号的,可以识别-符号,可能会处理溢出等。正确的答案是使用strtoul等函数。
string s("123");
unsigned u = (unsigned)atoi(s.c_str());
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