/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include <stdlib.h>
#include <blc_stdlib.h>

/****************************************************************************/
/*						User API functions									*/
/****************************************************************************/

/** @addtogroup stdlib */
/**	@{ */

/**
*	Converts a 32-bit unsigned integer to ASCII in radix 2^N.
*
*	The number will be converted without leading zeroes or spaces and the
*	result is terminated by a 0 byte.
*
*	@param	buff	Pointer to the buffer where the result is stored.
*					At worst case the buffer needs to be 65 bytes long.
*	@param	numb	The unsigned long long integer to convert
*	@param	base	The conversion base, must be a power of 2 between 2 and 32.
*	@param	uppc	For bases 16 or 32 if this flag is true, then digits
*					over 9 will use uppercase letters. If the flag is false,
*					then lowercase will be used.
*
*	@return			The length of the final string, not counting the
*					terminating 0. If the radix is not a power of 2
*					between 2 and 32 inclusive, then no conversion takes
*					place and 0 is returned.
*
*	@note	This function is not a standard C library function.
*/

int		ulltostr2n( char *buff, unsigned long long numb,
					int base, int uppc )
{
unsigned long	upp, mid, low, len;

	if ( ! ( numb >> 32 ) ) {

		return( ultostr2n( buff, (unsigned long) numb, base, uppc ) );
	}

	switch ( base ) {

		case  8:

			upp = numb >> 33;
			low = numb & 0x3fffffff;
			mid = ( numb >> 30 ) & 7;
			break;

		case 32:

			upp = numb >> 35;
			low = numb & 0x3fffffff;
			mid = ( numb >> 30 ) & 31;
			break;

		case  2:
		case  4:
		case 16:

			len  = _blc_itoa2n( buff, numb >> 32, base, uppc, 0 );
			len += _blc_itoa2n( buff + len, numb, base, uppc, 1 );
			return( len );

		default:

			return( 0 );
	}

	if ( upp )

		len = _blc_itoa2n( buff, upp, base, uppc, 0 );
	else
		len = 0;

	upp = _blc_itoa2n( buff + len, low, base, uppc, 1 );
	low = ( uppc ? 'A' : 'a' ) - 10;
	buff[ len ] = mid + ( mid > 9 ? low : '0' );
	return( len + upp );
}

/** @} */

