static void _SplitParam(CString cszPara, const wchar_t* seps, CStringList &strListBox)
{
POSITION pos = strListBox.GetTailPosition();
wchar_t *token;
//wchar_t seps[] = L",; \t\n";
wchar_t cBuffer[200] ;
lstrcpy(cBuffer,(LPCTSTR)cszPara);
token = wcstok(cBuffer,seps);
while (TRUE)
{
if (token == 0)
break;
else
{
pos = strListBox.InsertAfter(pos, token);
token = wcstok(NULL,seps);
}
}
}
long PART1 = 0xff000000;
long PART2 = 0xff0000;
long PART3 = 0xff00;
long PART4 = 0xff;
/** 将IP地址长整型数值转化为IPv4字符串 */
CString IP2CString(long ip) {
CString ipStr("");
CString ipSub("");
ipSub.Format(_T("%d"),(ip & PART4));
ipStr += ipSub;
ipSub.Format(_T(".%d"),(ip & PART3) >> 8);
ipStr += ipSub;
ipSub.Format(_T(".%d"),(ip & PART2) >> 16);
ipStr += ipSub;
ipSub.Format(_T(".%d"),(ip & PART1) >> 24);
ipStr += ipSub;
return ipStr;
}
/** 将IPv4字符串转化为对应的长整型整数 */
DWORD IPCString2DWORD(CString ip) {
CStringList strListBox;
_SplitParam(ip,_T("\\."),strListBox);
if(strListBox.GetCount()<4)
return 0;
//CString[] p4 = ip.split("\\.");
DWORD ipInt = 0;
DWORD part = atoi((CW2A)strListBox.GetAt(strListBox.FindIndex(0)));
ipInt = ipInt | (part << 24);
part =atoi((CW2A)strListBox.GetAt(strListBox.FindIndex(1)));
ipInt = ipInt | (part << 16);
part = atoi((CW2A)strListBox.GetAt(strListBox.FindIndex(2)));
ipInt = ipInt | (part << 8);
part = atoi((CW2A)strListBox.GetAt(strListBox.FindIndex(3)));
ipInt = ipInt | (part);
return ipInt;
}
IPv4字符串与长整型数互转
最新推荐文章于 2024-04-22 00:16:22 发布