#include #include "string.h" #include "stdlib.h" #include "stddef.h" static void strreverse( char *s ) { char *end = s + strlen( s ) - 1; while( s < end ) { // XOR swap; *s ^= *end; *end ^= *s; *s ^= *end; s++; end--; } } char *itoa( int v, char *s, int base ) { static char digit[] = "0123456789ABCDEF"; bool sign = false; char *p = s; if( base < 2 || base > 16 ) { return NULL; } if( v < 0 ) { v = -v; sign = true; } do { *p++ = digit[v % base]; } while( ( v /= base ) > 0 ); if( sign ) { *p++ = '-'; } *p = '\0'; strreverse( s ); return s; }