How to Convert from Other Bases to Decimal and from Decimal to Octal and Hexadecimal on the Bash Shell

How to Convert from Other Bases to Decimal and from Decimal to Octal and Hexadecimal on the Bash Shell

Conversion of numbers from other bases to base 10, to base 8 and base 16

Play this article

Introduction

A number base is simply the number of unique digits used to represent a number in a system. A practical example is a dozen, which consists of 12 etc.

The most common one of these number bases is the Decimal or Denary as some will call it, which consists of digits from 0 to 9 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9). Any other number is gotten from the combination of two or more of these numbers. Other popular ones are the Binary numbering system (0, 1), the octal system (0, 1, 2, 3, 4, 5, 6, 7) and the hexadecimal system (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F).

Why Number Bases?

The system of number bases has its use in different fields like mathematics, cryptography and computer science etc.

In the field of computer science, they are used to represent data in memory and also used to perform calculations.

Number Base Conversion

Due to the nature of number bases, a number in one base can be converted into a number in another base. That is a particular number can have a different representation when used in another base, hence the need to convert numbers from one base to another base.

Today, you will learn how to convert a number from one base into another base using just the command shell.

Conversion from other bases to Decimal

To convert a number from any base to a number in decimal (base 10) in the shell terminal, we can simply use the # command:

echo $((base#number_in_the_base))

so practically, to convert a number in any given base to decimal, we simply use the command above.

Now let's say I want to convert a Binary number (base 2) to a number in decimal, let's say the binary number we want to convert is 1101101, we can do this by applying the command above:

echo $((2#1101101))

Once you press your enter button upon typing this command on your command shell terminal, it will return the number in decimal.

we can see that 1101101 in binary (base 2) is equivalent to 109 in decimal (base 10).

Let's say we want to convert the number 12ADF which is a hexadecimal (base 16) to a number in decimal (base 10), we can simply do that by using the same command:

echo $((16#12ADF))

You can see the result from the picture, you can simply use this method to convert from any base to a number in Decimal (base 10).

Conversion from Decimal to Octal and Hexadecimal

To convert from decimal (base 10) to octal (base 8) and hexadecimal (base 16), we need to use the printf command together with the text format for the base.

  1. Decimal to Octal

    To convert from decimal to octal, we use the text format for the octal, which is the %o. This is how it is been done:

     echo $(printf "%o" Decimal_to_be_converted)
    

    so here, the printf command simply follows the format provided, which is the %o and takes the number following the format to return the number in that format that is provided.

    For example, let us say we want to convert 528 to a number in octal (base 8);

     echo $(printf "%o" 528)
    

    From the image, we can see that 528 in decimal will give us 1020 in octal.

  2. Decimal to Hexadecial

    Just like how we were able to convert numbers from decimal to octal, so also using the same print command, we will be able to convert numbers from decimal to hexadecimal, this time around using the hexadecimal format %x . We can convert any number in decimal to a number in hexadecimal generally by using this command:

     echo $(printf "%x" Decimal_to_be_converted)
    

    For example, let us say we want to convert 725 which is in decimal, to a number in hexadecimal, using this command, we are going to do;

     echo $(printf "%x" 725)
    

    Here we can see that 725 in decimal is equivalent to 2d5 in hexadecimal.

    Note: The Terminal does not just decide to print out the letter d in lowercase, to print out the letters in hexadecimal in uppercase, then an upper X in the formatter should be used, as can be seen below;

     echo $(printf "%X" 725)
    

    Now we have our letter in uppercase as against the initial one in which it was in lowercase.

Conclusion

There are other methods of converting numbers from one base to another, methods like using the bc calculator command, the typeset command etc. which are also good, but are more technical. The approach in this article is the most simple, straight forward and direct method.

Thank you for reading through, please let's connect on Twitter and LinkedIn.