//pip 源
//pszip 输出ip
// 长度
void CForceIpDlg::strIpToNumberIp(const char* pIp, int* pszIp, int iSize)
{
if ( NULL == pszIp ||
iSize < 4 )
{
return;
}
std::string stdstrValue = pIp;
int i = 0;
::memset(pszIp, 0, (sizeof(int) * iSize));
while ( true )
{
std::string::size_type iPos = stdstrValue.find( "." );
if ( iPos == std::string::npos )
{
std::stringstream ss;
ss << stdstrValue;
int val = 0;
ss >> val;
pszIp[i++] = val;
break;
}
std::string strSub = stdstrValue.substr( 0, iPos );
stdstrValue = stdstrValue.substr( iPos+1 );
std::stringstream ss;
ss << strSub;
int val = 0;
ss >> val;
pszIp[i++] = val;
iPos = stdstrValue.find( "." );
}
}
excample:
int szIp[4] = {0};
//call
strIpToNumberIp("10.0.1.22", szIp, 4);