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

You can use the bc command-line calculator tool to convert between octal and hexadecimal number systems in Linux or UNIX systems. Here’s how:

Convert Octal to Hexadecimal:

$ echo "ibase=8;obase=16; <octal_number>" | bc

Where <octal_number> is the octal number you want to convert.

Convert Hexadecimal to Octal:

$ echo "ibase=16;obase=8; <hexadecimal_number>" | bc

Where <hexadecimal_number> is the hexadecimal number you want to convert.

Example:

$ echo "ibase=8;obase=16; 177" | bc
7F
$ echo "ibase=16;obase=8; 7F" | bc
177

In these examples, the ibase setting specifies the input base of the number (octal or hexadecimal), and the obase setting specifies the output base (hexadecimal or octal).

Leave a Comment