UNIX:如何将IP地址转换为二进制代码

发布于 2024-09-29 12:36:04 字数 100 浏览 4 评论 0原文

有没有命令可以将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兔小萌 2024-10-06 12:36:04

嗯,这是一种(非常复杂的)方法:

pax> export ip=10.110.11.116

pax> for i in $(echo ${ip} | tr '.' ' '); do echo "obase=2 ; $i" | bc; done
     | awk '{printf ".%08d", $1}' | cut -c2-
00001010.01101110.00001011.01110100

echo/tr 语句为您提供一个以空格分隔的八位字节列表,for 会按某个时间处理这些八位字节。时间。

对于每一个,您都将其传递给 bc,并将输出基数设置为 2(二进制)。然后,这四行可变长度二进制数通过 awk 将它们的大小强制为 8,将它们放回单行,并在每行前面加上 . 和最后的 cut 只是删除第一个 .

当然,我几乎可以肯定有更好的方法可以做到这一点,但这表明您可以通过一点点聪明才智和花费数十年的时间来使用 UNIX :-)

Well, here's one (very convoluted) way to do it:

pax> export ip=10.110.11.116

pax> for i in $(echo ${ip} | tr '.' ' '); do echo "obase=2 ; $i" | bc; done
     | awk '{printf ".%08d", $1}' | cut -c2-
00001010.01101110.00001011.01110100

The echo/tr statement gives you a space-separated list of the octets and the for 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 through awk to force them to a size of 8, put them back on a single line, and precede each with a . and the final cut 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 :-)

两人的回忆 2024-10-06 12:36:04

这是一种方法——但是二进制数字上没有前导零:

IP=192.168.4.254
echo $IP | tr '.' '\n' | while read octet
do
        echo "2 o $octet p" | dc
done | tr '\n' '.'

或者作为对 dc 的单个调用:

IP=192.168.4.254
echo $IP | tr '.' ' ' | while read octet1 octet2 octet3 octet4
do
        echo "2 o $octet1 p $octet2 p $octet3 p $octet4 p" | dc | tr '\n' '.'
done

Here's one way to do it -- no leading zeros on the binary digits however:

IP=192.168.4.254
echo $IP | tr '.' '\n' | while read octet
do
        echo "2 o $octet p" | dc
done | tr '\n' '.'

Or as a single call to dc:

IP=192.168.4.254
echo $IP | tr '.' ' ' | while read octet1 octet2 octet3 octet4
do
        echo "2 o $octet1 p $octet2 p $octet3 p $octet4 p" | dc | tr '\n' '.'
done
留蓝 2024-10-06 12:36:04

下面是一种无需任何外部实用程序即可在 Bash 中工作的方法:

tobin ()
{
    local val bits b c d;
    val=$1;
    bits="";
    (( val < 2 )) && bits=$val;
    b="";
    while (( val > 1 )); do
        bits=$b$bits;
        (( b = val % 2 ));
        (( c = ( val / 2 ) * ( val % 2 ) ));
        (( val = val / 2 ));
        (( d = val ));
    done;
    echo "$d$c$bits"
}

byte () { printf "%08d" $1; }

unset dot binary
saveIFS=$IFS
IFS=.
ip=($1)
IFS=saveIFS
for octet in ${ip[@]}
do
    binary=$binary$dot$(byte $(tobin $octet))
    dot=.
done
echo $binary

对于兼容 POSIX 的 Bourne shell:

tobin () {
    local val bits b c d
    val=$1
    bits=""
    if [ $val -lt 2 ]
    then
        bits=$val
    fi
    b=""
    while [ $val -gt 1 ]
    do
        bits=$b$bits
        b=$(($val%2))
        c=$((($val/2)*($val%2)))
        val=$(($val/2))
        d=$val
    done
    echo "$d$c$bits"
}

byte () { printf "%08d" $1; }    # printf may be an external utility in some shells

unset dot binary
saveIFS=$IFS
IFS=.
set -- $a
IFS=$saveIFS

for octet
do
    binary=$binary$dot$(byte $(tobin $octet))
    dot=.
done
echo $binary

Here's a way that will work in Bash without any external utilities:

tobin ()
{
    local val bits b c d;
    val=$1;
    bits="";
    (( val < 2 )) && bits=$val;
    b="";
    while (( val > 1 )); do
        bits=$b$bits;
        (( b = val % 2 ));
        (( c = ( val / 2 ) * ( val % 2 ) ));
        (( val = val / 2 ));
        (( d = val ));
    done;
    echo "$d$c$bits"
}

byte () { printf "%08d" $1; }

unset dot binary
saveIFS=$IFS
IFS=.
ip=($1)
IFS=saveIFS
for octet in ${ip[@]}
do
    binary=$binary$dot$(byte $(tobin $octet))
    dot=.
done
echo $binary

For a POSIX compliant Bourne shell:

tobin () {
    local val bits b c d
    val=$1
    bits=""
    if [ $val -lt 2 ]
    then
        bits=$val
    fi
    b=""
    while [ $val -gt 1 ]
    do
        bits=$b$bits
        b=$(($val%2))
        c=$((($val/2)*($val%2)))
        val=$(($val/2))
        d=$val
    done
    echo "$d$c$bits"
}

byte () { printf "%08d" $1; }    # printf may be an external utility in some shells

unset dot binary
saveIFS=$IFS
IFS=.
set -- $a
IFS=$saveIFS

for octet
do
    binary=$binary$dot$(byte $(tobin $octet))
    dot=.
done
echo $binary
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文