IPv4字符串与长整型数互转

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;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值