又如何将xxx.xxx.xxx.xxx类型的IP地址转换成int型呢?
解决方案 »
- 关于FreeImage库,望大家解答,谢谢
- 对话框图片重叠问题
- vc++中编译成功后运行的错误Runtime Error?
- 关于CDialog 非模态对话框
- 如何是对话框一创建,它上面的控件ComboBox就收到一个CBN_SELCHANGE消息?
- 求救:select语句提示错误“无效的描述器索引”
- 完成端口一问:关于使用HasOverlappedIoCompleted这个函数?
- 请问各位一个关于图象隐藏的问题?????
- 什么情况下用bezier(贝塞尔)曲面求值,什么情况用nurbs(非均匀有理B样条曲面)?
- SOS!工具栏上加控件问题,小女子先多谢了。。。。
- 关于任务管理器的问题?
- 如何在VC中加载指定的“C:\index.xml”文件?
struct in_addr in
);unsigned long inet_addr(
const char* cp
);
DWORD GetNetworkAddr(const char* addr)
{
if(addr == NULL)
return INADDR_NONE;
if(addr[0] == 0)
return INADDR_NONE; DWORD dwAddr = 0;
DWORD dwPart, dwLoop;
if(addr[1] == 0) // Unicode
{
WCHAR c;
const WCHAR* cp = (const WCHAR*)addr;
for(dwLoop = 0; dwLoop < 4; dwLoop++)
{
dwPart = 0;
while ((c = *cp++) != L'\0' && c != L'.') {
if (c < L'0' || c > L'9') // IP 地址中含有非法字符
return INADDR_NONE;
dwPart = dwPart * 10 + (c - L'0');
}
if(dwPart > 255) // IP 地址中某个段的内容大于 255
return INADDR_NONE;
dwAddr = dwAddr | (dwPart << dwLoop*8);
if (dwLoop == 3) {
if (c != L'\0')
return INADDR_NONE; // 给定的 IP 地址太长
} else {
if (c == L'\0')
return INADDR_NONE; // 给定的 IP 地址太短
}
}
}else{ // ASCII
char c;
const char* cp = (const char*)addr;
for (dwLoop = 0; dwLoop < 4; dwLoop++) {
dwPart = 0;
while ((c = *cp++) != '\0' && c != '.' && c != ',') {
if (c < '0' || c > '9') // IP 地址中含有非法字符
return INADDR_NONE;
dwPart = dwPart * 10 + (c - '0');
}
if (dwPart > 255) // IP 地址中某个段的内容大于 255
return INADDR_NONE;
dwAddr = dwAddr | (dwPart << dwLoop*8);
if (dwLoop == 3) {
if (c != '\0' && c != ',')
return INADDR_NONE; // 给定的 IP 地址太长
} else {
if (c == '\0' || c == ',')
return INADDR_NONE; // 给定的 IP 地址太短
}
}
}
return dwAddr;
}
DWORD ip = 16820416;
in_addr aa;
aa.S_un.S_addr = ip;
CString str = inet_ntoa(aa);
AfxMessageBox(str); //16820416 是192.168.0.1
引入头文件#include "Winsock2.h"
然后设置Project->setting->Object libary modules添加Ws2_32.lib 就可以了注意IP最好是使DWORD或者unsigned int
WSADATA wsaData;
int iRet = WSAStartup(MAKEWORD(0x02, 0x02), &wsaData);
if (iRet != 0)
{
TRACE("初始化winsock动态库出错!");
m_strIPAddress = "";
return;
} struct in_addr localaddr;
struct hostent *hp=NULL;
char hostname[50];
gethostname(hostname,49);//主机名 hp=gethostbyname(hostname);//主机信息
memcpy(&localaddr,hp->h_addr,hp->h_length);//地址 m_strIPAddress = inet_ntoa(localaddr);//变成char * WSACleanup();
in.S_un.s_addr = dwIP;
CString sIP = inet_ntoa(in);
题目:如何将int型的IP地址转换成xxx.xxx.xxx.xxx的格式?
内容:又如何将xxx.xxx.xxx.xxx类型的IP地址转换成int型呢?
到底想问哪个,还是都想问
u_long ip;
ip = inet_addr("192.168.0.1");
CString str;
str.Format("%d",ip);
AfxMessageBox(str);
这是内容所需要的代码,题目所需要的代码已经在上面了,别忘记给我加分,顺便再写一遍我这段代码,经过测试。
DWORD ip = 16820416;
in_addr aa;
aa.S_un.S_addr = ip;
CString str = inet_ntoa(aa);
AfxMessageBox(str); //16820416 是192.168.0.1
引入头文件#include "Winsock2.h"
然后设置Project->setting->Object libary modules添加Ws2_32.lib 就可以了注意IP最好是使DWORD或者unsigned long
需要手算我也可以告诉你,
有IP类型的组件!