在我们继续之前,我要感谢以下为解决方案作出贡献的人。这个函数是由以下人员贡献的:Stuart Lowe (本文作者),Robert Jan Schaper,Ray-Yuan Sheu, Rodrigo de Salvo Braz,Wes Garland,John Maloney,Brian Hunt,Fernando Corradi and Lukás Chmela。
演变过程
以下是早期的一个版本,由Robert Jan Schaper表述于Google groups:
char* version 0.1
1 2 3 4 5 6 7
char* itoa(int val, int base){ staticchar buf[32] = {0}; int i = 30; for(; val && i ; --i, val /= base) buf[i] = "0123456789abcdef"[val % base]; return &buf[i+1]; }
我所使用的版本和这个版本看起来不太一样,它更像是这样的形式:itoa(int value, char* buffer, int radix)。在最后,我给出了我自己使用std::string代替字符串的版本。
std::string version 0.1
1 2 3 4 5 6
voidmy_itoa(int value, std::string& buf, int base){ int i = 30; buf = ""; for(; value && i ; --i, value /= base) buf = "0123456789abcdef"[value % base] + buf; }
/** * C++ version char* style "itoa": */ char* itoa( int value, char* result, int base ) { // check that the base if valid if (base < 2 || base > 16) { *result = 0; return result; }
char* out = result; int quotient = value;
do { *out = "0123456789abcdef"[ std::abs( quotient % base ) ]; ++out; quotient /= base; } while ( quotient );
// Only apply negative sign for base 10 if ( value < 0 && base == 10) *out++ = '-'; std::reverse( result, out ); *out = 0; return result; }
/** * C++ version 0.4 char* style "itoa": * Written by Lukás Chmela * Released under GPLv3. */ char* itoa(int value, char* result, int base) { // check that the base if valid if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char; int tmp_value;
do { tmp_value = value; value /= base; *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789 abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; } while ( value );
/** * C++ version 0.4 std::string style "itoa": * Contributions from Stuart Lowe, Ray-Yuan Sheu, * Rodrigo de Salvo Braz, Luc Gallant, John Maloney * and Brian Hunt */ std::stringitoa(int value, int base) {
std::string buf;
// check that the base if valid if (base < 2 || base > 16) return buf;
/** * C++ version 0.4 char* style "itoa": * Written by Lukás Chmela * Released under GPLv3. */ char* itoa(int value, char* result, int base) { // check that the base if valid if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char; int tmp_value;
do { tmp_value = value; value /= base; *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789 abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; } while ( value );
char* style “itoa” (v 0.2) char* itoa(int value, char* result, int base)
1.0 (XP, Cygwin, g++)
char* style “itoa” (v 0.3) char* itoa(int value, char* result, int base)
0.93
char* style “itoa” (v 0.4) char* itoa(int value, char* result, int base)
0.72
Ansi C “itoa” based on Kernighan & Ritchie’s “Ansi C” with modification to optimize for specific architecture void itoa(int value, char* str, int base)
0.92
std::string style “itoa” (v 0.3) std::string itoa(int value, int base)
41.5
std::string style “itoa” (v 0.4) std::string itoa(int value, int base)