/**
* Converts a long int to its string representation.
* @param value The long integer value.
* @param base The integer's base.
* @return The long int's string represenation.
*/
static inline const std::string long2string( long int value, const int base = 10 )
{
int add = 0;
if( base < 2 || base > 16 || value == 0 )
return "0";
else if( value < 0 )
{
++add;
value = -value;
}
int len = (int)( log( (double)( value ? value : 1 ) ) / log( (double)base ) ) + 1;
const char digits[] = "0123456789ABCDEF";
char* num = (char*)calloc( len + 1 + add, sizeof( char ) );
num[len--] = '\0';
if( add )
num[0] = '-';
while( value && len > -1 )
{
num[len-- + add] = digits[(int)( value % base )];
value /= base;
}
const std::string result( num );
free( num );
return result;
}
阅读(1062) | 评论(0) | 转发(0) |