Convert integer to string in C without sprintf

Can I convert an integer to a string in C without sprintf ?

+4
source share
5 answers

You can use itoa where possible. If this is not available on your platform, the following implementation may be of interest:

https://web.archive.org/web/20130722203238/https://www.student.cs.uwaterloo.ca/~cs350/common/os161-src-html/atoi_8c-source.html

Using:

 char *numberAsString = itoa(integerValue); 

UPDATE

Based on R .. comments, it may be advisable to modify the existing itoa implementation to accept the result buffer from the caller, instead of allocating it and returning the buffer.

Such an implementation should accept both the buffer and the length of the buffer, without worrying about writing over the end of the buffer provided by the caller.

+4
source

There is a non-standard function:

 char *string = itoa(numberToConvert, 10); // assuming you want a base-10 representation 

Edit: it looks like you need some kind of algorithm. Here's how in base-10:

 #include <stdio.h> #define STRINGIFY(x) #x #define INTMIN_STR STRINGIFY(INT_MIN) int main() { int anInteger = -13765; // or whatever if (anInteger == INT_MIN) { // handle corner case puts(INTMIN_STR); return 0; } int flag = 0; char str[128] = { 0 }; // large enough for an int even on 64-bit int i = 126; if (anInteger < 0) { flag = 1; anInteger = -anInteger; } while (anInteger != 0) { str[i--] = (anInteger % 10) + '0'; anInteger /= 10; } if (flag) str[i--] = '-'; printf("The number was: %s\n", str + i + 1); return 0; } 
+6
source

Here is an example of how it can work. Given the buffer and size, we will divide by 10 and fill the buffer with numbers. We will return -1 if there is not enough space in the buffer.

 int integer_to_string(char *buf, size_t bufsize, int n) { char *start; // Handle negative numbers. // if (n < 0) { if (!bufsize) return -1; *buf++ = '-'; bufsize--; } // Remember the start of the string... This will come into play // at the end. // start = buf; do { // Handle the current digit. // int digit; if (!bufsize) return -1; digit = n % 10; if (digit < 0) digit *= -1; *buf++ = digit + '0'; bufsize--; n /= 10; } while (n); // Terminate the string. // if (!bufsize) return -1; *buf = 0; // We wrote the string backwards, ie with least significant digits first. // Now reverse the string. // --buf; while (start < buf) { char a = *start; *start = *buf; *buf = a; ++start; --buf; } return 0; } 
+5
source

Unfortunately, none of the answers above can really work in their pure form in a situation where you need to come up with a string of alphanumeric characters. There are really strange cases that I have seen, especially in interviews and at work.

The only bad part of the code is that you need to know the bounds of the whole so that you can properly assign the string.

Although C is considered predictable, it can have strange behavior in a large system if you get lost in coding.

The solution below returns an integer representation string with a null terminating character. It does not depend on any external functions and works with negative integers !!

 #include <stdio.h> #include <stdlib.h> void IntegertoString(char * string, int number) { if(number == 0) { string[0] = '0'; return; }; int divide = 0; int modResult; int length = 0; int isNegative = 0; int copyOfNumber; int offset = 0; copyOfNumber = number; if( number < 0 ) { isNegative = 1; number = 0 - number; length++; } while(copyOfNumber != 0) { length++; copyOfNumber /= 10; } for(divide = 0; divide < length; divide++) { modResult = number % 10; number = number / 10; string[length - (divide + 1)] = modResult + '0'; } if(isNegative) { string[0] = '-'; } string[length] = '\0'; } int main(void) { char string[10]; int number = -131230; IntegertoString(string, number); printf("%s\n", string); return 0; } 
+4
source
  int i = 24344; /*integer*/ char *str = itoa(i); /*allocates required memory and then converts integer to string and the address of first byte of memory is returned to str pointer.*/ 
0
source

All Articles