Jump to content
  • Advertisement
Sign in to follow this  
gerbenvv

Int to c-string

This topic is 4625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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
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

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!