如何将IP地址的十进制表示形式转换为二进制形式? [英] How can I convert the decimal representation of an IP address into binary?

查看:399
本文介绍了如何将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.

这篇关于如何将IP地址的十进制表示形式转换为二进制形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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