You won’t be challenged fairly often to transform numbers from one numbering system to a different however, if you end up, you are able to do it with both of two pretty simple instructions on the Linux command line.
Changing in your head might be taxing, particularly for longer numbers. Whereas the decimal numbering system permits any digit in a quantity to have any of ten values (0-9), digits in hexadecimal numbers can have 16 (0-F), digits in octal numbers solely eight (0-7) and digits in binary numbers solely two (0-1).
And, whether or not you prefer it or not, every now and then you’re more likely to run into numbers displayed in hex or octal, and understanding how one can convert them from one quantity base to a different can come in useful.
To get began, the decimal numbers 5 to 16 within the 4 numbering techniques seem like this:
5 6 7 8 9 10 11 12 13 14 15 16 <== decimal
5 6 7 8 9 a b c d e f 10 <== hexadecimal 5 6 7 10 11 12 13 14 15 16 17 20 <== octal 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 <== binary
Utilizing the printf command to transform decimal numbers
To transform a quantity from decimal to hexadecimal, you should use a printf command just like the one beneath that converts decimal 15 to hexadecimal:
$ printf ‘%xn’ 15 f
To transform to octal as a substitute, substitute the “%x” (hexadecimal) with “%o” (octal):
$ printf ‘%on’ 15 17
For the 17 proven within the second instance above, recall that the primary digit (1) has a decimal worth of 8 and the second (7) a worth of seven. Due to this fact, the octal consequence is the same as decimal 15.
Turning these conversions into scripts is straightforward. Listed here are two scripts that can immediate for the quantity to be transformed after which run the printf command to do the conversion for you.
dec2hex
#!/bin/bash echo -n “Enter a decimal quantity> “ learn quantity printf ‘%xn’ $quantity
dec2oct
#!/bin/bash echo -n “Enter a decimal quantity> “ learn quantity printf ‘%on’ $quantity
Word: The printf command doesn’t have an possibility for changing to binary.
Utilizing the bc command to transform decimal numbers
Utilizing the bc (calculator) command, you are able to do the identical sort of conversions with instructions like these proven beneath.
The primary instance converts the decimal quantity 16 to base 16 (hexadecimal). Since in hexadecimal the second digit from the best represents the 16s place (that’s the decimal 16), the 1 means decimal 16. The “obase” setting means “output base”. That is the format you need the quantity to be transformed to. As a result of the “ibase” (enter base) just isn’t offered, it’s assumed to be decimal.
$ echo “obase=16; 16” | bc 10
The following command converts that very same quantity to octal. Since, in octal, the second digit from the best represents the decimal 8s place, a 2 means decimal 16 (two 8s).
$ echo “obase=8; 16” | bc 20
With the subsequent bigger quantity, a decimal 17, we get 21—two decimal 8s and one 1 consequence.
$ echo “obase=8; 17” | bc 21
The bc command works simply as properly with binary because it does with hex and octal, so we will additionally convert numbers to binary. Right here’s the command to show decimal 16 as a binary quantity:
$ echo “obase=2; 16” | bc 10000
Described in base 10, that’s one 16, no 8s, no 4s, no 2s, and no 1s.
Whereas the binary system got here into use as a result of it really works straight with the on/off state of electronics, the octal and hexadecimal techniques present simple conversions in order that we people don’t have to take a look at lengthy strings of 0’s and 1’s. Even this doesn’t limit the operation of the bc command which has no downside working with a base 3, base 7 or another numbering base that you just may need to experiment with.
$ echo “obase=3; 16” | bc 121
Described as decimal numbers, the 121 within the above instance represents one 9, two 3’s and one 1. Right here’s the bottom 7 model:
$ echo “obase=7; 16” | bc 22
The digits above symbolize two decimal 7’s and two 1’s.
Changing numbers to decimal with bc
With all that stated, you too can use the bc command to show numbers expressed in hex, octal, or binary to decimal. Simply specify the obase (output base) worth as 10, the ibase (enter base) quantity as 16 if the beginning variety of hex, 8 if it’s octal and a couple of if it’s binary as within the examples proven beneath.
$ echo “obase=10; ibase=16; F” | bc 15 $ echo “obase=10; ibase=8; 15” | bc 13 $ echo “obase=10; ibase=2; 112” | bc 7
As within the earlier state of affairs, turning these instructions into scripts takes solely slightly effort.
hex2dec
#!/bin/bash echo -n “Sort a hex quantity> “ learn quantity echo “obase=10; ibase=16; $quantity” | bc
oct2dec
#!/bin/bash echo -n “Sort a octal quantity> “ learn quantity echo “obase=10; ibase=8; $quantity” | bc
bin2dec
#!/bin/bash echo -n “Sort a binary quantity> “ learn quantity echo “obase=10; ibase=16; $quantity” | bc
Utilizing aliases for number-base conversions
Whereas the scripts proven above are simple to arrange, most likely essentially the most handy option to implement any of the conversions proven is to set them up as a bunch of aliases. Including the alias beneath to your ~/.bashrc file would make them obtainable in your subsequent login or as quickly as you supply the file (e.g., by operating . ~/.bashrc).
# convert from decimal alias dec2hex=’f() bc; unset -f f; ; f’ alias dec2oct=’f() bc; unset -f f; ; f’ alias dec2bin=’f() bc; unset -f f; ; f’ # convert to decimal alias hex2dec=’f() bc; unset -f f; ; f’ alias oct2dec=’f() bc; unset -f f; ; f’ alias bin2dec=’f() bc; unset -f f; ; f’
Afterwards, you possibly can run the aliases like this:
$ dec2hex 15 F $ dec2oct 15 17 $ dec2bin 15 1111 $ hex2dec F 15 $ oct2dec 17 15 $ bin2dec 1111 15
Wrap-Up
Changing numbers from one numbering system to a different might be tedious, however it can save you your self lots of bother utilizing the printf and bc instructions, particularly in the event you set them up as scripts or aliases.
Copyright © 2022 IDG Communications, Inc.