Bracing against the wind  
www.documentroot.com  

Wednesday, January 20, 2010

itoa

With the same sort of mild insanity that afflicts many standards making bodies, itoa has been removed from gcc.
char* itoa(int val, char *buf, int base) {
int m = sizeof(int)*(base >=10 ? 4 : 8)-1;
int i = m;
int j = 0;

if (base > 100 || base < 2)
return NULL;

if (val < 0) {
val = -val;
buf[0]='-';
j = 1;
} else if (!val) {
buf[0]='0';
buf[1]='\0';
return buf;
}

for(; val && i ; --i, val /= base)
buf[i] = '0' + (val % base);

for (;i < m; ++i, ++j) {
buf[j]=buf[i+1];
}

buf[j]='\0';
return buf;
}

[View/Post Comments] [Digg] [Del.icio.us] [Stumble]

Home | Email me when this weblog updates: | View Archive

(C) 2002 Erik Aronesty/DocumentRoot.Com. Right to copy, without attribution, is given freely to anyone for any reason.


Listed on BlogShares | Bloghop: the best pretty good | Blogarama | Technorati | Blogwise