This one-liner will convert binary numbers to decimal. Very easy to do in the bash shell.
jason@jason-desktop:~$ echo "$((2#01110100011000010110101101))" 30508461 |
This is how to convert binary to Hexadecimal.
jason@jason-desktop:~$ printf '%x\n' "$((2#1111111))" 7f |
These very useful one-liners should make converting number values even easier.
But if you want to convert large numbers, you can use bc.
This example will convert a large decimal number into binary. This converts from base 10 to base 2.
┌──[jason@192.168.1.2]─[~] └──╼ ╼ $ echo 'obase=2; ibase=10; 1234376534785346534653426534536656575676776867867857655476756657567667567465346534765346563' | bc |
And to convert a large number from binary to decimal, use this technique. Simply converting base 2 to base 10. This would be a very useful technique for Cisco students. That uses a lot of binary numbers.
┌──[jason@192.168.1.2]─[~] └──╼ ╼ $ echo 'obase=10; ibase=2; 1001101100100000101001101110000100111100101110100111010101010' | bc 1397264720181284522 |
This example is converting a number in decimal to octal base 8.
┌──[jason@192.168.1.2]─[~] └──╼ ╼ $ echo 'obase=8; ibase=10; 1234567890' | bc 11145401322 |
Here is another example of this technique. Converting the decimal number ‘1234’ to binary.
jason@jason-Lenovo-H50-55:~$ echo 'obase=2; ibase=10; 1234' | bc 10011010010 |
Then converting the binary value to decimal again.
jason@jason-Lenovo-H50-55:~$ echo $((2#10011010010)) 1234 |
This could be very useful in networking. Learning binary numbers can be very useful to someone studying Cisco networking, but in practice, it is good to have a shortcut to help when calculating binary values.
Another way is to use the ipcalc utility. This can perform the conversion easily.
jason@jason-Lenovo-H50-55:~$ ipcalc 192.168.1.0/24 Address: 192.168.1.0 11000000.10101000.00000001. 00000000 Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000 Wildcard: 0.0.0.255 00000000.00000000.00000000. 11111111 => Network: 192.168.1.0/24 11000000.10101000.00000001. 00000000 HostMin: 192.168.1.1 11000000.10101000.00000001. 00000001 HostMax: 192.168.1.254 11000000.10101000.00000001. 11111110 Broadcast: 192.168.1.255 11000000.10101000.00000001. 11111111 Hosts/Net: 254 Class C, Private Internet |
Bruh it works for small binary numbers, if number is larger it throwing error