Skip to main content
GameDev.net gamedev.net
🔒 Locked

Int to c-string

Started by gerbenvv Oct 23, 2005 at 8:18 AM 17 replies 3.5k views
Original Post
gerbenvv
gerbenvv
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
while (!asleep()) { sheep++;}
SiCrane
SiCrane
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.
Kuladus
Kuladus
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.
gerbenvv
gerbenvv
Tnx, I'm gonna try it.
No, I don't use the std classes at all.
while (!asleep()) { sheep++;}
Peregrin
Peregrin
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.
Jooleem. Get addicted.
furby100
furby100
You didn't add one for the null terminator! Eek!
Peregrin
Peregrin
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.
Jooleem. Get addicted.
Zahlman
Zahlman
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.


dyerseve
dyerseve
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
shermy101
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 number = 543;int size = 1;int temp = number;while (temp != 0) {size++;temp /= 10;}	char buffer[size];sprintf(buffer, "%d", number);
Kuladus
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?
Wavarian
Wavarian
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]
Zahlman
Zahlman
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]
SiCrane
SiCrane
C99 allows declarations at any point in a function, not just at the top of a scope.
Dragoncar
Dragoncar
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?
SiCrane
SiCrane
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.
Kuladus
Kuladus
Unless you do

maxsize = ceiling (sizeof(int) * log(10))
shermy101
shermy101
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.