来自gloox库的long型数据转换为字符串

来源:互联网 发布:华为p9耗电 优化 编辑:程序博客网 时间:2024/05/29 07:43

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

原创粉丝点击