简体   繁体   English

替代itoa()的C ++标准,用于将int转换为以10为基数的字符*

[英]C++ standard alternative to itoa() to convert int to base 10 char*

To convert an integer to base 10 char* 将整数转换为以10为基数的字符*

std::itoa(ConCounter, ID, 10);

ConCounter is an integer, ID is a char*, and 10 is the base ConCounter是一个整数,ID是一个char *,而10是基数

It says that iota is not a member of std and without std it's not declared. 它说iota不是std的成员,没有std则不会声明。 I know it's a nonstandard function but I included all the libraries for it and it still doesn't see it. 我知道这是一个非标准函数,但我包括了所有库,但仍然看不到。

What is a way to do the above? 以上是什么方法? Any quick one liners? 任何快速的衬板? I've tried the following; 我尝试了以下方法;

std::to_string //it's not declared for me when using mingw, it doesn't exist.
snprintf/sprintf //should work but it gives me the "invalid conversion from 'int' to 'char *'"    error
std::stoi //has same problem as iota

Try this: 尝试这个:

#include <sstream>

int i = // your number
std::ostringstream digit;
digit<<i;
std::string numberString(digit.str());

I recommend using roybatty's answer, but I think sprintf should work too. 我建议使用roybatty的答案,但我认为sprintf也应该起作用。 I think when you used it you forgot the format string. 我认为使用它时您忘记了格式字符串。 It should be: 它应该是:

char buf[16];
std::snprintf(buf, sizeof(buf), "%d", integer);

还有“ strtol”功能: http ://www.cplusplus.com/reference/cstdlib/strtol/

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

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