I want to know how to get number to string without standard C or C++ functions, for example:
char str[20];
int num = 1234;
// How to convert that number to string (str)?
Thanks.
I want to know how to get number to string without standard C or C++ functions, for example:
char str[20];
int num = 1234;
// How to convert that number to string (str)?
Thanks.
Assuming you're preallocating your buffer for str as in your question:
char *itostr(int num, char *str) {
int len = 1;
long tmp = num;
int sign = num < 0;
if (sign) {
str[0] = '-';
tmp = -tmp;
}
while (num/=10) ++len;
str[len+sign] = 0;
while (len--) {
str[len+sign] = '0'+tmp%10;
tmp /= 10;
}
return str;
}
INT_MIN.sizeof long > sizeof int.)sizeof long == sizeof int then declare tmp to be long long (C99 and later).sizeof long long > size int, but not when they are the same size. Certainly true in most implementations.#define smart conditional handling of them! Cheers.An "after accepted answer" that works for all int including INT_MIN.
static char *intTostring_helper(int i, char *s) {
if (i < -9) {
s = intTostring_helper(i/10, s);
}
*s++ = (-(i%10)) + '0' ;
return s;
}
char *intTostring(int i, char *dest) {
char *s = dest;
if (i < 0) { // If non 2s compliment, change to some IsSignBitSet() function.
*s++ = '-';
}
else {
i = -i;
}
s = intTostring_helper(i, s);
*s = '\0';
return dest;
}
A simplistic way to do this is to leave a lot of leading zeros. I like it because it uses only basic code, and doesn't require any dynamic memory allocation. It should consequently also be very fast:
char * convertToString(int num, str) {
int val;
val = num / 1000000000; str[0] = '0' + val; num -= val * 1000000000;
val = num / 100000000; str[1] = '0' + val; num -= val * 100000000;
val = num / 10000000; str[2] = '0' + val; num -= val * 10000000;
val = num / 1000000; str[3] = '0' + val; num -= val * 1000000;
val = num / 100000; str[4] = '0' + val; num -= val * 100000;
val = num / 10000; str[5] = '0' + val; num -= val * 10000;
val = num / 1000; str[6] = '0' + val; num -= val * 1000;
val = num / 100; str[7] = '0' + val; num -= val * 100;
val = num / 10; str[8] = '0' + val; num -= val * 10;
val = num; str[9] = '0' + val;
str[10] = '\0';
return str;
}
Of course, there are tons of tweaks you could do to this - modifying the way the destination array gets created is possible, as is adding a boolean that says to trim leading 0s. And we could make this much more efficient using a loop. Here's in improved method:
void convertToStringFancier(int num, char * returnArrayAtLeast11Bytes, bool trimLeadingZeros) {
int divisor = 1000000000;
char str[11];
int i;
int val;
for (i = 0; i < 10; ++i, divisor /= 10) {
val = num / divisor;
str[i] = '0' + val;
num -= val * divisor;
}
str[i] = '\0';
// Note that everything below here is just to get rid of the leading zeros and copy the array, which is longer than the actual number conversion.
char * ptr = str;
if (trimLeadingZeros) {
while (*ptr == '0') { ++ptr; }
if (*ptr == '\0') { // handle special case when the input was 0
*(--ptr) = '0';
}
for (i = 0; i < 10 && *ptr != '\0'; ++i) {
while (*ptr != '\0') {
returnArrayAtLeast11Bytes[i] = *ptr;
}
returnArrayAtLeast11Bytes[i] = '\0';
}
static keyword that accomplishes this. Do note, of course, that the result is not thread-safe in the sense that two threads will collide in their use of the same private storage.