# Int to c-string

## Recommended Posts

gerbenvv    116
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:
int  number = 109;
char string[ LENGTH ];
sprintf(string, "%d", number);


Gerben VV

##### Share on other sites
SiCrane    11839
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.

##### Share on other sites
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.

##### Share on other sites
gerbenvv    116
Tnx, I'm gonna try it.
No, I don't use the std classes at all.

##### Share on other sites
Peregrin    496
You could figure out what's the maximum number of digits the integer can take by using the log10 function.

#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.

##### Share on other sites
furby100    102
You didn't add one for the null terminator! Eek!

##### Share on other sites
Peregrin    496
Quote:
 Original post by furby100You 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.

##### Share on other sites
Zahlman    1682
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 SiCraneIf you are using C++ then you can use std::stringstream or boost::lexical_cast with std::string and not worry about memory allocation.

##### Share on other sites
dyerseve    194
Quote:
 Original post by gerbenvvTnx, I'm gonna try it.No, I don't use the std classes at all.

##### Share on other sites
shermy101    127
Quote:
 Original post by gerbenvvTnx, 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);

##### Share on other sites
Quote:
Original post by shermy101
Quote:
 Original post by gerbenvvTnx, 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 size = 1;int temp = number;while (temp != 0) {size++;temp /= 10;}	char buffer[size];

##### Share on other sites
Wavarian    850
Quote:
Quote:
Original post by shermy101
Quote:
 Original post by gerbenvvTnx, 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 size = 1;int temp = number;while (temp != 0) {size++;temp /= 10;}	char buffer[size];

[lol]

##### Share on other sites
Zahlman    1682
Quote:
Quote:
Original post by shermy101
Quote:
 Original post by gerbenvvTnx, 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 size = 1;int temp = number;while (temp != 0) {size++;temp /= 10;}	char buffer[size];

That buffer declaration is legal in C (as of C99), as a VLA - but (never mind; thanks SiCrane). Anyway, the size calculation there fails for 0 (1; should be 2 - the digit and the null terminator), and I imagine it will fail for negative values too (failing to account for the minus sign). Not to mention it's a silly amount of work just to avoid at most ten bytes on the stack. (But again, use proper tools like stringstream, etc. when you have them.)

[Edited by - Zahlman on October 24, 2005 6:51:13 PM]

##### Share on other sites
SiCrane    11839
C99 allows declarations at any point in a function, not just at the top of a scope.

##### Share on other sites
Dragoncar    517
Wouldn't a simple solution just be to use

sizeof(int)

and then that would let you set the array to whatever size was needed for an int on that system.

Or is it important that you get the exact number of digits in the integer?

##### Share on other sites
SiCrane    11839
sizeof(int) is 4 on my computer. A string containing the digits that fits in an int can be up to 12 characters long. sizeof is not a good approach here.

##### Share on other sites
Unless you do

maxsize = ceiling (sizeof(int) * log(10))

##### Share on other sites
shermy101    127
Quote:
Quote:
Original post by shermy101
Quote:
 Original post by gerbenvvTnx, 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 size = 1;int temp = number;while (temp != 0) {size++;temp /= 10;}	char buffer[size];

was just off the top of my head. advice as opposed to insult usually helps newbies like myself.

##### Share on other sites
Guest Anonymous Poster
Quote:
 Original post by KuladusUnless you domaxsize = ceiling (sizeof(int) * log(10))

more precisely it would be ceiling(8*sizeof(int)*log(2)/log(10)) as the solution to 2^(sizeof(int)*8) = 10^x