C++ - convert string/numerics

Moki Lv6

To string

1
2
int number = 123;
std::string converted = std::to_string(number);

To numeric

1
2
3
4
int number = 123;
char buffer[10];
std::sprintf(buffer, "%d", number);
std::string str(buffer);
1
2
float n;
sscanf("12345.00000123", "%f", &n);
1
2
int n;
sscanf("12345", "%d", &n);
1
2
unsigned int n;
sscanf("12345", "%ud", &n);
On this page
C++ - convert string/numerics