如何将ascii转换为unsigned int

时间:2009-09-30 07:13:50

标签: c++

是否有将字符串转换为unsigned int的方法? _ultoa存在但找不到vise诗歌版本......

5 个答案:

答案 0 :(得分:15)

std::strtoul()就是那个。然后又有像atoi()这样的旧版本。

答案 1 :(得分:11)

Boost提供lexical_cast。

#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;

答案 2 :(得分:1)

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

答案 3 :(得分:0)

如果你通过_atoi64

运行它

unsigned long l = _atoi64(str);

答案 4 :(得分:-2)

int atoi ( const char * str )怎么样?

string s("123");
unsigned u = (unsigned)atoi(s.c_str());