1. Introduction

Binary, hexadecimal, and decimal are numbering systems used to represent values in computers. Especially when it comes to programming, we might often want to convert between these number systems.

In this tutorial, we explore ways to convert binary numbers to hexadecimal and decimal numbers in a shell script.

2. Using printf

printf is a built-in command in Bash to format and print data according to a specified format string. Therefore, we can use the printf command to convert numbers.

Let’s use it in a script we called code.sh:

$ cat code.sh
#!/bin/bash
bin_number="$1"
hex_number=$(printf "%x" "$((2#$bin_number))")
dec_number=$(printf "%d" "$((2#$bin_number))")
echo "Binary Number: $bin_number"
echo "Hex Number: $hex_number"
echo "Decimal Number: $dec_number"

In this case, we used the printf command with the %x format specifier to get our result as a hexadecimal number. Moreover, %d is used to get the result in a decimal format.

Afterward, we used $((2#$bin_number)) to tell bash to evaluate the expression inside the parentheses. Then, the 2# prefix defines that $bin_number is a binary number.

Now, let’s check the code result:

$ bash code.sh 101010
Binary Number: 101010
Hex Number: 2a
Decimal Number: 42

Finally, the output is exactly what we expected.

3. Using bc

The bc command can use a mathematical expression to convert an input number to other formats.

First, let’s install it via yum:

$ yum install bc

Now, we can use it in our script:

$ cat code.sh
#!/bin/bash
bin_number="$1"
hex_number=$(echo "obase=16;ibase=2;$bin_number" | bc)
dec_number=$(echo "obase=10;ibase=2;$bin_number" | bc)
echo "Binary Number: $bin_number"
echo "Hex Number: $hex_number"
echo "Decimal Number: $dec_number"

Here, we used the echo command to print a string as the piped input of the bc command. Moreover, the ibase parameter specifies the input base as 2 for binary. In addition, the obase parameter specifies the output base. In this case, we used 16 for hexadecimal and 10 for decimal.

By piping these values along with the binary number as a properly formatted bc expression, we get back the result from a subshell and into the $hex_number and $dec_number variables:

$ bash code.sh 101010
Binary Number: 101010
Hex Number: 2a
Decimal Number: 42

Again, the result is correct.

4. Using Bash

We can also convert binary numbers to other formats by using Bash built-in commands alone.

4.1. Binary to Decimal

For the binary to decimal conversion, we use a loop over each digit of the binary number:

$ cat bin_to_dec.sh
#!/bin/bash
bin_number="$1"
dec_number=0
position=1
# Loop through the binary digits from right to left
for (( i=${#bin_number}-1; i>=0; i-- )); do
  digit="${bin_number:$i:1}"
  if [ "$digit" == "1" ]; then
    dec_number=$(( dec_number + position ))
  fi
  position=$(( position * 2 ))
done
echo "Decimal Number: $dec_number"

Here, we multiply each digit by the appropriate power of 2. Next, we add up all the resulting values to get the decimal equivalent.

4.2. Binary to Hexadecimal

On the other hand, for binary to hex conversion, we use loops through the binary digits in groups of four:

$ cat bin_to_hex.sh
#!/bin/bash
bin_number="$1"
hex_number=""

# Loop the binary digits in groups of 4
for (( i=0; i<${#bin_number}; i+=4 )); do
  # Get the current 4-bit segment of the binary number
  segment="${bin_number:i:4}"
  # Convert the segment to its hex digit
  case $segment in
    0000) hex_digit="0" ;;
    0001) hex_digit="1" ;;
    0010) hex_digit="2" ;;
    0011) hex_digit="3" ;;
    0100) hex_digit="4" ;;
    0101) hex_digit="5" ;;
    0110) hex_digit="6" ;;
    0111) hex_digit="7" ;;
    1000) hex_digit="8" ;;
    1001) hex_digit="9" ;;
    1010) hex_digit="A" ;;
    1011) hex_digit="B" ;;
    1100) hex_digit="C" ;;
    1101) hex_digit="D" ;;
    1110) hex_digit="E" ;;
    1111) hex_digit="F" ;;
  esac
  # Append the hex digit to the output string
  hex_number="${hex_number}${hex_digit}"
done
echo "Hex number: $hex_number"

Then, we use a case statement to convert each 4-bit segment to its hex equivalent. Finally, we append the resulting digit to the output string.

Notably, the maximum value that we can store in a Bash variable is 63 bits in length.

5. Conclusion

In this article, we explored various ways to convert binary numbers to hexadecimal and decimal numbers in the shell.

Firstly, we learned how to use the printf command for our needs. Secondly, we saw that the bc command uses a special syntax to convert numbers. Finally, we used Bash alone for our purposes.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.