类型转换问题 CString转int
CString stwr("0xd2d34");
怎么将0xd2d34复制到const int aaa; 里面
结果是 int aaa=0xd2d34;
[解决办法]
CString stwr;
stwr="0xd2d34";
const int aaa=_ttoi(stwr);
ANSI 和 UNICODE 都可以用的代码
[解决办法]
#include <iostream>
#include <assert.h>
using namespace std;
const char* filteZero( const char *pStr )
{
assert( pStr );
const char *p = pStr;
while( *p != '/0' && *p == '0' )
p++;
return p;
}
void filteSpace( const char *&pStr )
{
assert( pStr );
while ( *pStr != '/0' && *pStr ==' ' )
pStr++;
}
int getSign( const char* &pStr )
{
if( *pStr == '-' && *pStr != '/0' )
{
pStr = pStr + 1;
return -1;
}
else
{
if( *pStr == '+')
pStr ++;
return 1;
}
}
int _atoi(const char *pStr )
{
assert(pStr);
int rlt = 0;
enum _state{ safe, check, overflow } state = safe;
filteSpace(pStr);
int sign = getSign( pStr );
pStr = filteZero( pStr );
while ( *pStr <= '9' && *pStr >= '0')
{
if ( state == check )
{
if ( sign > 0 && *pStr > '7' )
{
rlt = INT_MAX;
}
else if ( sign < 0 && *pStr > '8' )
{
rlt = INT_MIN;
}
else rlt = 10 * rlt + (*pStr - '1') + 1;
break;
}
if ( state == overflow )
{
rlt = sign > 0 ? INT_MAX : INT_MIN;
break;
}
rlt = 10 * rlt + (*pStr - '1') + 1;
pStr ++;
if ( sign > 0 )
{
if ( rlt < INT_MAX / 10 )
state = safe;
else if ( rlt = INT_MAX / 10 )
state = check;
else
state = overflow;
}
if ( sign < 0 )
{
if ( rlt > INT_MIN / 10 )
state = safe;
else if ( rlt == INT_MIN /10 )
state = check;
else
state = overflow;
}
}
return rlt * sign;
}
void main()
{
char *p = "-21474837880s";
assert( atoi(p) == _atoi(p) );
cout << _atoi(p) << endl;
system("pause");
}
atoi函数的实现原理。