C++ standard alternative to itoa() to convert int to base 10 char*
Domanda
To convert an integer to base 10 char*
std::itoa(ConCounter, ID, 10);
ConCounter is an integer, ID is a char*, and 10 is the base
It says that iota is not a member of std and without std it's not declared. 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
Soluzione
I recommend using roybatty's answer, but I think sprintf should work too. I think when you used it you forgot the format string. It should be:
char buf[16];
std::snprintf(buf, sizeof(buf), "%d", integer);
Altri suggerimenti
Try this:
#include <sstream>
int i = // your number
std::ostringstream digit;
digit<<i;
std::string numberString(digit.str());
There is also "strtol" function : http://www.cplusplus.com/reference/cstdlib/strtol/
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow