. Advertisement .
..3..
. Advertisement .
..4..
C++ is an incredibly powerful programming language, but it’s also one of the most barebones. You have to manipulate everything from the ground up, making it vital to learn lots of techniques. One of them is how to convert int to char array in C++.
As long as you understand the four approaches we will show, you won’t have any problem anymore.
Convert Int to Char Array in C++ With std::printf
The easiest and most approachable solution to this issue is to use an in-memory buffer to store the conversion’s output. In other words, we will do two things at once.
To do so, the first thing you need to perform is space allocation. Remember to keep it just enough to store one single variable of type int. This variable is what we will use as the buffer to convert into char.
If you are not sure about the length of the char buffer, you can use sizeof()
to calculate it. After all, std::printf
has an automatic habit of /0 byte termination when arriving at the destination. That is the exact reason you should do proper space allocation for the buffer.
A quick workaround is to use max_digit
to define the max length.
#include <iostream>
#define max_digit 8
int main() {
int n = 123;
char num[max_digit + sizeof(char)];
std::sprintf(num, "%d", n);
std::printf("num: %s \n", num);
return 0;
}
Output:
num: 123
You do need to keep in mind that sprintf can produce wrong results depending on which compiler you use.
Convert Int to Char Array in C++ By Combining to_string() and c_str()
As you can see from the word “combining”, it doesn’t rely on one single function like the method above. In return for this inconvenience, you get more safety, as you are not dealing with the sprintf function.
#include <iostream>
int main() {
int n = 123;
std::string temp = std::to_string(n);
char const *num = temp.c_str();
printf("num: %s \n", num);
return 0;
}
Output:
num: 123
Convert Int to Char Array in C++ Using std::stringstream
Instead of using a variable and be careful about the length and limit, this method utilizes the std::stringstream class.
To be more specific, you create a new, temporary stream of string, then store all the int data into it. Afterward, you simply return the string value using str and perform the final conversion with the help of c_str()
.
#include <iostream>
#include <sstream>
int main() {
int n = 123;
std::stringstream temp; temp << n;
char const *num = tmep.str().c_str();
printf("num: %s \n", num);
return 0;
}
There is no change to the result. Once you master this method, you can check its more advanced application of creating a string array.
Convert Int to Char Array in C++ Using std::to_chars
This approach is the most modern, being added in alongside the C++17 update in the <charconv>
header. Due to it being the newest, it has a lot of new techniques, allowing a much higher flexibility.
#include <iostream>
#include <charconv>
#define max_digit 8
int main() {
int n = 123;
char num[max_digit + sizeof(char)];
std::to_chars(num, num + MAX_DIGITS, n);
std::printf("num: %s \n", num);
return 0;
}
The result is the same.
Conclusion
All in all, there are a total of four approaches you can use to convert int to char array in C++. Each of them has unique strengths and weaknesses. However, we do want to stress that the first method should not be abused, as it has the highest risk.
If you find our guide interesting, look forward to other publications.
Leave a comment