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

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

To convert octal to hexadecimal or vice versa in Linux or Unix, you can use the bc command, which is a calculator utility that can perform arithmetic and numerical conversions.

To convert an octal number to hexadecimal using bc, you can use the obase and ibase options to set the output base and the input base, respectively. For example, to convert the octal number 123 to hexadecimal, you can use the following command:

refe‮l:ot r‬autturi.com
echo "obase=16; ibase=8; 123" | bc

This will output the hexadecimal equivalent of the octal number 123, which is 1b.

To convert a hexadecimal number to octal using bc, you can use the same obase and ibase options, but with different base values. For example, to convert the hexadecimal number 1b to octal, you can use the following command:

echo "obase=8; ibase=16; 1b" | bc

This will output the octal equivalent of the hexadecimal number 1b, which is 123.

Keep in mind that bc expects the numbers to be in base 10 by default, so if you are using numbers that are not in base 10, you must specify the input base using the ibase option.

Created Time:2017-10-16 14:38:52  Author:lautturi