如何将IP地址的十进制表示形式转换为二进制形式?
问题描述:
有人知道如何在Java中将IP地址的十进制表示法转换为二进制形式吗?请让我知道...
Does anyone knows how to convert decimal notation of an IP address into binary form in Java? Please let me know...
答
写为a.b.c.d
的IP地址可以转换为32位整数值
使用 shift 和 bit-明智的包容性OR 运算符,
An IP address written as a.b.c.d
can be converted to a 32-bit integer value
using shift and bit-wise inclusive OR operators as,
(a << 24) | (b << 16) | (c << 8) | d
为了安全起见,每个a,b,c,d
的有效范围都为0-255
-您可以在转换中进行检查.
您可以使用此正则表达式示例进一步 验证IP地址.
To be safe, each of a,b,c,d
has valid range 0-255
-- you can check that in your conversion.
You can further validate the IP address using this regex example.