Linux / UNIX: bc Convert Octal To Hexadecimal or Vise Versa

How do I convert from octal to hex number using the bc UNIX / Linux command line utility?

The octal numeral system is a base 8 numeral system. It uses the numerals 0 through 7. The octal numeral system is a base 16 numeral system. The standard numeral system is called base 10 (decimal) and uses ten symbols: 0,1,2,3,4,5,6,7,8,9. Hexadecimal uses the decimal numbers and includes six extra symbols. There are no symbols that mean ten, or eleven etc. so these symbols are letters taken from the English alphabet: A, B, C, D, E and F. Hexadecimal A = decimal 10, and hexadecimal F = decimal 15.


You need to use the following bc syntax to covert number:

# Convert octal to hexa ##
echo "obase=16; ibase=8; octal-number-here" | bc
## convert base 8 (octal) number 17 to hexadecimal ##
echo "obase=16; ibase=8; 17" | bc

Sample outputs:



  • obase = Set output base (like 2, 8, 10, 16)
  • ibase = Set input base (like 2, 8, 10, 16)
  • 17 = Convert this number to ibase. The input must be given as per obase.

To convert hexadecimal to octal use the following syntax:

# Convert hexa to  octal ##
echo "obase=8; ibase=16; hex-number-here" | bc
## convert base 16 (hex) number 10 to octal ##
echo "obase=8; ibase=16; 100" | bc

Sample outputs:


The following will convert whole file of octal to hex, one per line:
( echo "obase=16; ibase=8" ; cat octal-data-file.txt ) | bc

printf command example

You can also use the printf command as follows:

## Convert hex # 0x100 to octal base ##
printf "%o\n" 0x100

Sample outputs:



## Convert octal # 4242 to hex base ##
printf "%x\n" 04242

Sample output:


Please note that a hex base number must prefix with 0x and an octal base number must prefix with 0.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Sylwester Dec 12, 2017 @ 12:39

    Is it possible to do it the other way around ? Hexa to Octa ?

    • 🐧 Vivek Gite Dec 12, 2017 @ 17:10

      Yes to convert HEXA # 1388 to Octa, run:
      echo "obase=8; ibase=16; 1388" | bc
      a) ibase == input base set to hexa (16)
      b) obase == output base set to Octa (8)
      c) 1388 == it is an input number in hexa.


Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum