IP地址、CString和DWORD之间的转换

 

#pragma comment(lib,"ws2_32.lib")

#include <Winsock2.h>

//将CString 型IP地址在IPAddressCtrl中显示
CString strIP="192.168.0.10";
DWORD dwIP;
dwIP = inet_addr(strIP);
unsigned char *pIP = (unsigned char*)&dwIP;
m_ipAddr.SetAddress(*(pIP+3), *(pIP+2), *(pIP+1), *pIP);

//将IPAddressCtrl中的IP地址获得并转换成CString型
unsigned char *pIP;
CString strIP;
DWORD dwIP;
m_ipAddr.GetAddress(dwIP);
pIP = (unsigned char*)&dwIP;
strIP.Format("%u.%u.%u.%u",*(pIP+3), *(pIP+2), *(pIP+1), *pIP);

或者有一个简单的:

CString strIP="192.168.1.184";
DWORD dwAddress= ntohl( inet_addr(strIP));
m_IPAddr.SetAddress(dwAddress);

 

 

// DWORD IP转换为CString

DWORD ServerIP;

CString m_strServerIP

m_ServerIPCtrl.GetAddress(ServerIP);

m_strServerIP.Format(_T("%d.%d.%d.%d"),

(ServerIP>>24)&0xff,

(ServerIP>>16)&0xff,

(ServerIP>>8)&0xff,

ServerIP&0xff ) ;

posted @ 2012-09-17 13:45  飞飞O(∩_∩)  阅读(787)  评论(0编辑  收藏  举报