Bash shell 十进制到二进制基数 2 的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10278513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash shell Decimal to Binary base 2 conversion
提问by Daniel Del Core
I'm looking for an easy way in Bash to convert a decimal number into a binary number. I have variables that need to be converted:
我正在 Bash 中寻找一种简单的方法将十进制数转换为二进制数。我有需要转换的变量:
$ip1 $ip2 $ip3 $ip4
Is there a simple method to do this without looking at every individual number?
有没有一种简单的方法可以在不查看每个数字的情况下做到这一点?
I would prefer not to have to write a lot of code.
我宁愿不必编写大量代码。
回答by Cyrus
Convert decimal to binary with bash builtin commands (range 0 to 255):
使用 bash 内置命令(范围 0 到 255)将十进制转换为二进制:
D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
echo ${D2B[7]}
00000111
00000111
echo ${D2B[85]}
01010101
01010101
echo ${D2B[127]}
01111111
01111111
To remove leading zeros, e.g. from ${D2B[7]}
:
要删除前导零,例如从${D2B[7]}
:
echo $((10#${D2B[7]}))
111
111
This creates an array with 00000000 00000001 00000010 ... 11111101 11111110 11111111
with bash‘s brace expansion. The position in array D2B represents its decimal value.
这将创建一个00000000 00000001 00000010 ... 11111101 11111110 11111111
带有 bash 的大括号扩展的数组。数组 D2B 中的位置表示其十进制值。
See also: Understanding code ({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
另请参阅:理解代码({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0 ..1})
回答by J?rg Weilbier
General method for converting an integer number into another representation with another base (but base<=10 because of using digits 0..9 for representation, only):
将整数转换为另一种基数表示的一般方法(但基数<=10,因为仅使用数字 0..9 进行表示):
function convertIntvalToBase () # (Val Base)
{
val=
base=
result=""
while [ $val -ne 0 ] ; do
result=$(( $val % $base ))$result #residual is next digit
val=$(( $val / $base ))
done
echo -n $result
}
e.g.
例如
convertIntvalToBase $ip1 2 # converts $ip1 into binary representation
回答by Eric Leschinski
Decimal to binary conversion in Bash:
Bash中十进制到二进制的转换:
I'm using Ubuntu 14.04 to do this.
我正在使用 Ubuntu 14.04 来做到这一点。
Convert the decimals 1 through 5 to binary.
将十进制 1 到 5 转换为二进制。
el@apollo:~$ bc <<< "obase=2;1"
1
el@apollo:~$ bc <<< "obase=2;2"
10
el@apollo:~$ bc <<< "obase=2;3"
11
el@apollo:~$ bc <<< "obase=2;4"
100
el@apollo:~$ bc <<< "obase=2;5"
101
Bonus example:
奖金示例:
el@apollo:~$ bc <<< "obase=2;1024"
10000000000
el@apollo:~$ bc <<< "obase=2;2^128"
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
回答by Rakib Fiha
Decimal to Binary using only Bash
仅使用 Bash 将十进制转换为二进制
Any integer number can be converted ti binary using it::
任何整数都可以使用它转换为二进制:
touch dec2bin.bash && chmod +x "$_" && vim "$_"
And, then copy paste the following:
然后,复制粘贴以下内容:
#!/bin/bash
num=;
dec2bin()
{
op=2; ## Since we're converting to binary
quo=$(( $num/ $op)); ## quotient
rem=$(( $num% $op)); ## remainder
array=(); ## array for putting remainder inside array
array+=("$rem"); ## array expansion
until [[ $quo -eq 0 ]]; do
num=$quo; ## looping to get all remainder, untill the remainder is 0
quo=$(( $num / $op));
rem=$(( $num % $op));
array+="$rem"; ## array expansion
done
binary=$(echo "${array[@]}" | rev); ## reversing array
printf "$binary\n"; ## print array
}
main()
{
[[ -n ${num//[0-9]/} ]] &&
{ printf "$num is not an integer bruv!\n"; return 1;
} || { dec2bin $num; }
}
main;
For example:
例如:
./dec2bin.bash $var
110100100
Integer must be added!!
必须加整数!!
./dec2bin.bash 420.py
420.py is not an integer bruv!
Also, another wayusing python: Much slower
另外,另一种使用python的方法:慢得多
python -c "print(bin(420))"
0b110100100
Hexadecimal to Binary using only Bash
仅使用 Bash 将十六进制转换为二进制
Similarly, hexadecimal to binary, as follows using only bash:
同理,十六进制转二进制,如下只用bash:
#!/usr/local/bin/bash ## For Darwin :( higher bash :)
#!/bin/bash ## Linux :)
hex=;
hex2bin()
{
op=2; num=$((16#$hex));
quo=$(( $num/ $op));
rem=$(( $num% $op));
array=();
array+=("$rem");
until [[ $quo -eq 0 ]]; do
num=$quo;
quo=$(( $num / $op));
rem=$(( $num % $op));
array+="$rem";
done
binary=$(echo "${array[@]}" | rev);
printf "Binary of is: $binary\n";
}
main()
{
[[ -n ${hex//[0-9,A-F,a-f]/} ]] &&
{ printf "$hex is not a hexa decimal number bruv!\n"; return 1;
} || { hex2bin $hex; }
}
main;
For example:
例如:
./hex2bin.bash 1aF
Binary of 1aF is: 110101111
Hex must be passed:
必须通过十六进制:
./hex2bin.bash XyZ
XyZ is not a hexa decimal number bruv!