正常可见IP 为 127.0.0.1
实际存储为整形就是将IP的四个整数放到整形对应的四个字节上
#define _IP_MARK "."
//IP转换成int
unsigned int IPtoINT(const string & strIP)
{
unsigned int nRet = 0;
char chBuf[16] = "";
memcpy_s(chBuf, 15, strIP.c_str(), 15);
char* szBufTemp = NULL;
char* szBuf = strtok_s(chBuf,_IP_MARK,&szBufTemp);
int i = 0;//计数
while(NULL != szBuf)//取一个
{
nRet += atoi(szBuf)<<((3-i)*8);
szBuf = strtok_s(NULL,_IP_MARK,&szBufTemp);
i++;
}
return nRet;
}
//int转换成IP
string INTtoIP(uint32_t num)
{
string strRet = "";
for (int i=0;i<4;i++)
{
uint32_t tmp=(num>>((3-i)*8))&0xFF;
char chBuf[8] = "";
_itoa_s(tmp, chBuf, 10);
strRet += chBuf;
if (i < 3)
{
strRet += _IP_MARK;
}
}
return strRet;
}