Beispiel #1
0
/*------------------------------------------------------------------------------
 function name:	itoa
 description:		converts signed integer to an char array. Valid 'base' in [2;16].
 						Only base == 10 values are treated as signed.
 parameters:		value for converting, the output buffer, conversion base
 returned value:	pointer to the output buffer
------------------------------------------------------------------------------*/
char* itoa (int value, char* buffer, int base)
{
	static const char digits [] = "0123456789ABCDEF";
	char* tmpBuffer = buffer;
	int sign = 0;
	int quot, rem;

	if ( (base >= 2) && (base <= 16) )					/* check if the base is valid */
	{
		if (base == 10 && (sign = value) <0) 		/* check base & sign of value in the tmp copy */
		{
			value = -value;
		}

		do										/* calculate quotient & reminder */
		{
			quot = value / base;
			rem = value % base;
			*buffer ++ = digits[rem];				/* append the reminder to the string */
		} while ((value = quot));

		if (sign < 0)							/* if negative value add a sign */
		{
			*buffer++ = '-';
		}

		__reverse(tmpBuffer, buffer - 1);		/* reverse the string */
	}

	*buffer = '\0';
	return tmpBuffer;
}
Beispiel #2
0
Datei: btree.c Projekt: sktwj/var
static inline void __reverse(struct bnode *bnode)
{
	//树为空,或者遍历到叶子节点为止
	if (!bnode || ((!bnode->lchild) && (!bnode->rchild))) {
		return;
	}

	struct bnode *tmp;

	tmp = bnode->lchild;
	bnode->lchild = bnode->rchild;
	bnode->rchild = tmp;

	__reverse(bnode->lchild);
	__reverse(bnode->rchild);
}
Beispiel #3
0
char *itoa(int value, char *buffer, int base)
{
    static const char digits[] = "0123456789abcdef";

    char *buffer_copy = buffer;
    int32_t sign = 0;
    int32_t quot, rem;

    if ((base >= 2) && (base <= 16))        // is the base valid?
    {
        if (base == 10 && (sign = value) < 0)// negative value and base == 10? store the copy (sign)
            value = -value;                 // make it positive

        do
        {
            quot = value / base;            // calculate quotient and remainder
            rem = value % base;
            *buffer++ = digits[rem];        // append the remainder to the string
        }
        while ((value = quot));             // loop while there is something to convert

        if (sign < 0)                       // was the value negative?
            *buffer++ = '-';                // append the sign

        __reverse(buffer_copy, buffer - 1);     // reverse the string
    }

    *buffer = '\0';
    return buffer_copy;
}
Beispiel #4
0
/*!
* Előjeles egészet karakter tömbbé konvertál.
* @param[in]	value	 		Az átalakítandó egész szám.
* @param[out]	buffer	 		Itt keletkezik az eredmény.
* @param[in]	base	 		Alap (2; 10; 16 lehet).
* @param[in]	decimals		Tizedesek száma.
* @param[in]	expectedLength	Kívánt karakterhosszúság.
* @param[in]	padding_char	Kitöltő karakter.
* @param[in]	prefix			Előtag, mely a karakterlánc elejéhez fűződik.
* @param[in]	suffix			Utótag, mely a karakterlánc végéhez fűződik.
* @return		Mutató az eredmény bufferre.
*/
char* itoa (int value, char* buffer, int base, int decimals,
		    int expectedLength, char padding_char, char * prefix, char * suffix) //, char * prefix, char * suffix
{
	static const char digits [] = "0123456789ABCDEF";
	char* tmpBuffer = buffer;
	int sign = 0;
	int quot, rem;
	int dec = 1;
	int prefLength = 0;

	if ( (base >= 2) && (base <= 16) )					/* check if the base is valid */
	{
		if ( base == 10 && (sign = value) < 0) 		/* check base & sign of value in the tmp copy */
		{
			value = -value;
		}

		while ( suffix && *suffix )
		{
			*buffer ++ = *suffix;
			suffix++;
		}

		do										/* calculate quotient & reminder */
		{
			quot = value / base;
			rem = value % base;
			*buffer ++ = digits[rem];				/* append the reminder to the string */
			if (decimals == dec)
			{
				*buffer ++ = '.';
			}
			dec ++;
		} while ( (value = quot) );

		if( (sign < base) && (base == 10) && (decimals == 1) )
		{
			*buffer ++ = '0';
		}

		if ( sign < 0 )							/* if negative value add a sign */
		{
			*buffer++ = '-';
		}

		while ( (prefix) && (*(prefix + prefLength)) )
		{
			prefLength++;
		}

		while ( (buffer - tmpBuffer + prefLength) < expectedLength ) // + prefLength
		{
			*buffer++ = padding_char;
		}

		while ( prefix && *prefix )
		{
			*buffer++ = *prefix;
			prefix++;
		}

		__reverse(tmpBuffer, buffer - 1);		/* reverse the string */
	}

	*buffer = '\0';
	return tmpBuffer;
}
Beispiel #5
0
Datei: btree.c Projekt: sktwj/var
static inline void reverse(struct btree *btree)
{
	__reverse(btree->root);
}