CVS: msp430-libc/src/stdlib ltoa.c,NONE,1.1 ultoa.c,NONE,1.1 utoa.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-serv24012

Added Files:
	ltoa.c ultoa.c utoa.c 
Log Message:
itoa() now displays 0 properly.  Added ltoa, utoa, and ultoa

--- NEW FILE: ltoa.c ---
/*******************************************************
* Code contributed by Chris Takahashi,                 *
* ctakahashi (at) users (dot) sourceforge (dot) net.   *
* See stdlib.h for licence.                            *
* $Date: 2004/08/23 20:23:15 $                                             *
*******************************************************/
#include <stdlib.h>

char *ltoa(long num, char *str, int radix) {
	char sign = 0;
	char temp[33];	//an int can only be 32 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.
	do {
		digit = (unsigned int)num % radix;
		if (digit < 10) 
			temp[temp_loc++] = digit + '0';
		else
			temp[temp_loc++] = digit - 10 + 'A';
		((unsigned int)num) /= radix;
	} while ((unsigned int)num > 0);

	//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;
}


--- NEW FILE: ultoa.c ---
/*******************************************************
* Code contributed by Chris Takahashi,                 *
* ctakahashi (at) users (dot) sourceforge (dot) net.   *
* See stdlib.h for licence.                            *
* $Date: 2004/08/23 20:23:15 $                                             *
*******************************************************/
#include <stdlib.h>

char *ultoa(unsigned long num, char *str, int radix) {
	char temp[33];	//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;

	//construct a backward string of the number.
	do {
		digit = (unsigned int)num % radix;
		if (digit < 10) 
			temp[temp_loc++] = digit + '0';
		else
			temp[temp_loc++] = digit - 10 + 'A';
		((unsigned int)num) /= radix;
	} while ((unsigned int)num > 0);

	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;
}


--- NEW FILE: utoa.c ---
/*******************************************************
* Code contributed by Chris Takahashi,                 *
* ctakahashi (at) users (dot) sourceforge (dot) net.   *
* See stdlib.h for licence.                            *
* $Date: 2004/08/23 20:23:15 $                                             *
*******************************************************/
#include <stdlib.h>

char *utoa(unsigned num, char *str, int radix) {
	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;

	//construct a backward string of the number.
	do {
		digit = (unsigned int)num % radix;
		if (digit < 10) 
			temp[temp_loc++] = digit + '0';
		else
			temp[temp_loc++] = digit - 10 + 'A';
		((unsigned int)num) /= radix;
	} while ((unsigned int)num > 0);

	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;
}




-------------------------------------------------------
SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.