//----------------------------------------------------------------------------// // GNU GPL OS/K // // // // Authors: spectral` // // NeoX // // // // Desc: Conversion utilities - itoa family // //----------------------------------------------------------------------------// #include // // Digits table for bases <=36 // static const char digits[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // // Integer to string in any base between 2 and 36 (included) // #if defined(_NEED_ITOA) char *itoa(int i, char *str, int base) #elif defined(_NEED_LTOA) char *ltoa(long i, char *str, int base) #elif defined(_NEED_UTOA) char *utoa(uint i, char *str, int base) #elif defined(_NEED_ULTOA) char *ultoa(ulong i, char *str, int base) #else #error "What am I supposed to declare?" #endif { #if defined(_NEED_ITOA) || defined(_NEED_LTOA) int neg = 0; #endif char *orig = str; if (base < 2 || base > 36) return NULL; #if defined(_NEED_ITOA) || defined(_NEED_LTOA) // deal with negatives if (i < 0) { neg = 1; i = -i; } #endif // deal with zero separately if (i == 0) { *str++ = '0'; } // compute digits... in reverse order while (i > 0) { *str++ = digits[i % base]; i /= base; } #if defined(_NEED_ITOA) || defined(_NEED_LTOA) if (neg) *str++ = '-'; #endif *str = '\0'; return reverse(orig); }