Sign in to follow this  
gerbenvv

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 this post


Link to post
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 this post


Link to post
Share on other sites
Kuladus    380
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 this post


Link to post
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 digit

char* 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 this post


Link to post
Share on other sites
Peregrin    496
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.

Share this post


Link to post
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 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.


Share this post


Link to post
Share on other sites
shermy101    127
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);



Share this post


Link to post
Share on other sites
Kuladus    380
Quote:
Original post by shermy101
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 size = 1;
int temp = number;
while (temp != 0) {size++;temp /= 10;}

char buffer[size];



Where'd you get your C license?

Share this post


Link to post
Share on other sites
Wavarian    850
Quote:
Original post by Kuladus
Quote:
Original post by shermy101
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 size = 1;
int temp = number;
while (temp != 0) {size++;temp /= 10;}

char buffer[size];



Where'd you get your C license?


[lol]

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Kuladus
Quote:
Original post by shermy101
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 size = 1;
int temp = number;
while (temp != 0) {size++;temp /= 10;}

char buffer[size];



Where'd you get your C license?


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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
shermy101    127
Quote:
Original post by Kuladus
Quote:
Original post by shermy101
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 size = 1;
int temp = number;
while (temp != 0) {size++;temp /= 10;}

char buffer[size];



Where'd you get your C license?


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Kuladus
Unless you do


maxsize = 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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this