Saturday, April 9, 2022

[SOLVED] Convert decimal to hexadecimal in UNIX shell script

Issue

In a UNIX shell script, what can I use to convert decimal numbers into hexadecimal? I thought od would do the trick, but it's not realizing I'm feeding it ASCII representations of numbers.

printf? Gross! Using it for now, but what else is available?


Solution

echo "obase=16; 34" | bc

If you want to filter a whole file of integers, one per line:

( echo "obase=16" ; cat file_of_integers ) | bc


Answered By - Bill Karwin
Answer Checked By - David Marino (WPSolving Volunteer)