繁体   English   中英

如何将ascii转换为unsigned int

[英]how to convert ascii to unsigned int

有没有将字符串转换为unsigned int的方法? _ultoa存在但无法找到vise版本...

std::strtoul()是其中之一。 然后又有像atoi()这样的旧版本。

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;

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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM