Convert decimal to hexadecimal in UNIX shell script

skiphoppy picture skiphoppy · Dec 18, 2008 · Viewed 194.3k times · Source

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?

Answer

Keltia picture Keltia · Dec 18, 2008

Tried printf(1)?

printf "%x\n" 34
22

There are probably ways of doing that with builtin functions in all shells but it would be less portable. I've not checked the POSIX sh specs to see whether it has such capabilities.