IP地址转换为整数 [英] Conversion of IP address to integer

查看:417
本文介绍了IP地址转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
   IP地址整数 - ç

我如何把一个IP地址,以整数形式如下:
A.B.的 的或A.B.C. *或A.的 的或*的 的。*。*
我想写一个C程序,可以判断一个IP地址是另一个的子集。例如IP地址192.168.125.5是192.168一个子集。的 的。现在我想两者的IP地址转换为唯一的整数,并随后检查是否一个是另一个的子集。有没有办法实现这一目标?

How do I convert an IP address to integer which has the following form: A.B.. or A.B.C.* or A...* or ..*.* I want to write a C program which can determine if an IP address is a subset of another. e.g the IP address 192.168.125.5 is a subset of 192.168... Now I want to convert both the IP addresses to unique integers and subsequently check if one is a subset of the other. Is there a way to achieve this?

推荐答案

这是IP地址,例如192.168.0.1可以用十六进制变成将0xC0 0xA8为0x00为0x01或只是0xC0A80001

An IP address, e.g. 192.168.0.1 can be written as an integer easily by writing it in hex which becomes 0xC0 0xA8 0x00 0x01 or just 0xC0A80001

然后,它只是位匹配的情况下,让你建立一个相应的子网掩码255.255.0.0这只是0xFFFF0000地址,你可以测试都匹配这样做:

Then it's just a case of bit matching, so you construct a corresponding subnet mask, 255.255.0.0 which is just 0xFFFF0000 and you can test that both match by doing:

0xC0A80001 & 0xFFFF0000 == 0xC0A80000 & 0xFFFF0000

在这里平等的测试一边是你关心的东西基地,另一边是一个真正的地址,与给定的面具是双方一致的。

where one side of the equality test is the "base stuff you care about" and the other side is a real address, with a given mask being the same on both sides.

在一般的,你可以通过设置事情是建立一个合适的面具*为0,东西是实际数字是0xFF的,如果你想更精细的面具,很难与前preSS'*'

In general you can construct a suitable mask by setting things which are '*' to be 0 and things which are actual numbers to be 0xFF, if you want finer grain masks it's hard to express with '*'.

这篇关于IP地址转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆