先转载再说
string Int64AsString( __int64 value )
{
if ( value == 0 )
{
return string(0);
}
bool isNegative = false;
string result;
if ( value<0 )
{
result += string("-");
value = -value;
isNegative = true;
}
int flag = 10;
int int64BitMax = 20;
int bitRecord = 1;
for ( int bit=1; bit<=int64BitMax; bit++ )
{
__int64 flagValue = power( flag, bit );
//找出是第几位数
if ( value/flagValue == 0 )
{
bitRecord = bit;
break;
}
}
//从最高位开始
for ( bit=bitRecord; bit>=1; bit-- )
{
__int64 flagValue = power( flag, bit );
int bitValue = (value - value/flagValue*flagValue)/(flagValue/10);
result += IntAsString(bitValue);
}
return result;
}
__int64 StringAsInt64( const string & value )
{
string valueCopy(value);
__int64 result = 0;
int flag = 10;
bool isNegative = false;
if ( value[0] == '-' )
{
//负数
valueCopy.erase( valueCopy.begin() );
isNegative = true;
}
if ( isNegative )
{
//太大
if ( value.size()>=20 )
{
//出错
return -1;
}
}
else
{
//太大
if ( value.size()>=19 )
{
//出错
return -1;
}
}
for ( int bit=0; bit<valueCopy.size(); bit++ )
{
__int64 bitValue = power( flag, valueCopy.size()-bit-1 );
//0的asic码是48,字符'1',(int)'1'后,得到49,再减去48
result += bitValue*((int)valueCopy[bit]-48);
}
if ( isNegative )
{
result = -result;
}
return result;
}
//value的index次方(不处理负数)
__int64 power( int value, int index )
{
if ( index == 0 )
{
return 1;
}
//如果小于0或是超出了__int64的最大位数
if ( index<0 || index>=19 )
{
#ifdef _DEBUG
ASSERT(0);
#endif
return -1;
}
__int64 result = 1;
for ( int i=1; i<=index; i++ )
{
result *= value;
}
return result;
}
string Int64AsString( __int64 value )
{
if ( value == 0 )
{
return string(0);
}
bool isNegative = false;
string result;
if ( value<0 )
{
result += string("-");
value = -value;
isNegative = true;
}
int flag = 10;
int int64BitMax = 20;
int bitRecord = 1;
for ( int bit=1; bit<=int64BitMax; bit++ )
{
__int64 flagValue = power( flag, bit );
//找出是第几位数
if ( value/flagValue == 0 )
{
bitRecord = bit;
break;
}
}
//从最高位开始
for ( bit=bitRecord; bit>=1; bit-- )
{
__int64 flagValue = power( flag, bit );
int bitValue = (value - value/flagValue*flagValue)/(flagValue/10);
result += IntAsString(bitValue);
}
return result;
}
__int64 StringAsInt64( const string & value )
{
string valueCopy(value);
__int64 result = 0;
int flag = 10;
bool isNegative = false;
if ( value[0] == '-' )
{
//负数
valueCopy.erase( valueCopy.begin() );
isNegative = true;
}
if ( isNegative )
{
//太大
if ( value.size()>=20 )
{
//出错
return -1;
}
}
else
{
//太大
if ( value.size()>=19 )
{
//出错
return -1;
}
}
for ( int bit=0; bit<valueCopy.size(); bit++ )
{
__int64 bitValue = power( flag, valueCopy.size()-bit-1 );
//0的asic码是48,字符'1',(int)'1'后,得到49,再减去48
result += bitValue*((int)valueCopy[bit]-48);
}
if ( isNegative )
{
result = -result;
}
return result;
}
//value的index次方(不处理负数)
__int64 power( int value, int index )
{
if ( index == 0 )
{
return 1;
}
//如果小于0或是超出了__int64的最大位数
if ( index<0 || index>=19 )
{
#ifdef _DEBUG
ASSERT(0);
#endif
return -1;
}
__int64 result = 1;
for ( int i=1; i<=index; i++ )
{
result *= value;
}
return result;
}