UNIX:如何将IP地址转换为二进制代码
有没有命令可以将ip地址转换为二进制形式?
例如:10.110.11.116
输出:00001010.01101110.00001011.01110100
Is there is any Command to convert ip address in to binary form?
Eg: 10.110.11.116
output: 00001010.01101110.00001011.01110100
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,这是一种(非常复杂的)方法:
echo/tr
语句为您提供一个以空格分隔的八位字节列表,for
会按某个时间处理这些八位字节。时间。对于每一个,您都将其传递给
bc
,并将输出基数设置为 2(二进制)。然后,这四行可变长度二进制数通过awk
将它们的大小强制为 8,将它们放回单行,并在每行前面加上.
和最后的cut
只是删除第一个.
。当然,我几乎可以肯定有更好的方法可以做到这一点,但这表明您可以通过一点点聪明才智和花费数十年的时间来使用 UNIX :-)
Well, here's one (very convoluted) way to do it:
The
echo/tr
statement gives you a space-separated list of the octets and thefor
processes these one at a time.For each one, you pass it through
bc
with the output base set to 2 (binary). Those four lines of variable length binary numbers then go throughawk
to force them to a size of 8, put them back on a single line, and precede each with a.
and the finalcut
just removes the first.
.I'm almost certain there are better ways to do this of course but this shows what you can do with a bit of ingenuity and too many decades spent playing with UNIX :-)
这是一种方法——但是二进制数字上没有前导零:
或者作为对 dc 的单个调用:
Here's one way to do it -- no leading zeros on the binary digits however:
Or as a single call to dc:
下面是一种无需任何外部实用程序即可在 Bash 中工作的方法:
对于兼容 POSIX 的 Bourne shell:
Here's a way that will work in Bash without any external utilities:
For a POSIX compliant Bourne shell: