itoa implement for c


/ Published in: C
Save to your folder(s)

A implementation for itoa with c.
itoa is function that convert a integer to a string.


Copy this code and paste it in your HTML
  1. /**
  2.  * C++ version 0.4 char* style "itoa":
  3.  * Written by Lukás Chmela
  4.  * Released under GPLv3.
  5. */
  6.  
  7. char* itoa_c(int value, char* result, int base)
  8. {
  9. // check that the base if valid
  10. if ( base < 2 || base > 36 ) {
  11. *result = '\0';
  12. return result;
  13. }
  14.  
  15. char* ptr = result, *ptr1 = result, tmp_char;
  16. int tmp_value;
  17.  
  18. do {
  19. tmp_value = value;
  20. value /= base;
  21. *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
  22. } while ( value );
  23.  
  24. // Apply negative sign
  25. if ( tmp_value < 0 )
  26. *ptr++ = '-';
  27. *ptr-- = '\0';
  28.  
  29. while ( ptr1 < ptr ) {
  30. tmp_char = *ptr;
  31. *ptr-- = *ptr1;
  32. *ptr1++ = tmp_char;
  33. }
  34.  
  35. return result;
  36. }

URL: http://www.strudel.org.uk/itoa/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.