我已经使用winapi函数inet_ntoa将无符号的十进制长整数转换为ip地址。我正在反向获取ip地址。我使用了网站http://www.allredroster.com/iptodec.htm来获取小数等效于输入的IP地址。
for ex decimal equivalent of 206.117.16.66 is 3463778370
如果我使用函数inet_ntoa取回IP,则其IP为66.16.117.206。以下是相同的代码,请让我知道正确的方法。
#include<stdio.h>
#include<WinSock2.h>
#pragma comment(lib, "Ws2_32.lib")
int main()
{
ULONG ipdec=0;
struct in_addr ipadr;
char ip[50];
printf("\n Enter the ip address in decimal equivalent of ip address : ");
scanf_s("%ld",&ipdec);
ipadr.S_un.S_addr=ipdec;
strcpy_s(ip,inet_ntoa(ipadr));
printf("\n The ip address in dotted format is : %s \n" ,ip);
}
inet_ntoa
被指定为以网络字节顺序获取其输入。您以主机字节顺序提供输入,在x86系统上该输入是向后的(小字节序与大字节序)。您需要先传递十进制地址htonl
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句