int number = 109;
char string[ LENGTH ];
sprintf(string, "%d", number);
Int to c-string
Hi,
I'm trying to convert an int into a c-string with a sprintf() call. But I need to know the length of the c-string first for buffer overflows.
Or just: I need to know LENGTH in this source:
Gerben VV
If you have a C99 compliant snprintf() implementation, you can call snprintf() with a null buffer to get how many characters would be written by a format string with given arguments.
If you are using C++ then you can use std::stringstream or boost::lexical_cast with std::string and not worry about memory allocation.
If you are using C++ then you can use std::stringstream or boost::lexical_cast with std::string and not worry about memory allocation.
Pick a number. Any number greater than 3 will do.
For safety you could pick LENGTH to be, say 20, which is longer than the longest int.
For safety you could pick LENGTH to be, say 20, which is longer than the longest int.
You could figure out what's the maximum number of digits the integer can take by using the log10 function.
That this is just off the top of my head, and totally untested. There might be off-by-one errors hiding in the code.
Note that this technique is just a curiosity and I would not recommend using it in an actualy product. I would go with Kuladus and advise you to just pick a reasonable number.
#include <limits.h>#include <math.h>...int max_chars_in_int = (int) log10((double) INT_MAX);// You might need to add 1 for the sign digitchar* string = (char *) malloc (max_chars_in_int);
That this is just off the top of my head, and totally untested. There might be off-by-one errors hiding in the code.
Note that this technique is just a curiosity and I would not recommend using it in an actualy product. I would go with Kuladus and advise you to just pick a reasonable number.
Quote:Original post by furby100
You didn't add one for the null terminator! Eek!
Good point. Although I did warn this was a proof-of-concept, uncompiled, untested and unverified. As I said, there might be a bunch of off-by-one issues that need to be addressed: does the result of log10 need to be rounded up? How about a space for the number's sign character? And, as you said, an extra char for the null terminator.
You could just use the maximum size that an integer could possibly take up (assuming a 32-bit int size) - ten digits, plus negative sign, plus null terminator == 12. Or,
Quote:Original post by SiCrane
If you are using C++ then you can use std::stringstream or boost::lexical_cast with std::string and not worry about memory allocation.
Quote:Original post by gerbenvv
Tnx, I'm gonna try it.
No, I don't use the std classes at all.
Thats your mistake :-p
Quote:Original post by gerbenvv
Tnx, I'm gonna try it.
No, I don't use the std classes at all.
here is one approach to not using std classes..
int number = 543;int size = 1;int temp = number;while (temp != 0) {size++;temp /= 10;} char buffer[size];sprintf(buffer, "%d", number);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement