CVS: msp430-libc/src/stdlib itoa.c,NONE,1.1
Chris Takahashi <[email protected]>
| Newsgroups | gmane.comp.hardware.texas-instruments.msp430.gcc.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/mspgcc/msp430-libc/src/stdlib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10864/src/stdlib
Added Files:
itoa.c
Log Message:
Added char *itoa(int number, char *str, int radix); function to stdlib.
--- NEW FILE: itoa.c ---
/*******************************************************
* Code contributed by Chris Takahashi, *
* ctakahashi (at) users (dot) sourceforge (dot) net. *
* See stdlib.h for licence. *
* $Date: 2004/07/02 19:09:31 $ *
*******************************************************/
#include <stdlib.h>
char *itoa(int num, char *str, int radix) {
char sign = 0;
char temp[17]; //an int can only be 16 bits long
//at radix 2 (binary) the string
//is at most 16 + 1 null long.
int temp_loc = 0;
int digit;
int str_loc = 0;
//save sign for radix 10 conversion
if (radix == 10 && num < 0) {
sign = 1;
num = -num;
}
//construct a backward string of the number.
while ((unsigned int)num > 0) {
digit = (unsigned int)num % radix;
if (digit < 10)
temp[temp_loc++] = digit + '0';
else
temp[temp_loc++] = digit - 10 + 'A';
((unsigned int)num) /= radix;
}
//now add the sign for radix 10
if (radix == 10 && sign) {
temp[temp_loc] = '-';
} else {
temp_loc--;
}
//now reverse the string.
while ( temp_loc >=0 ) {// while there are still chars
str[str_loc++] = temp[temp_loc--];
}
str[str_loc] = 0; // add null termination.
return str;
}
-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
digital self defense, top technical experts, no vendor pitches,
unmatched networking opportunities. Visit www.blackhat.com