Last Updated: February 25, 2016
·
970
· cengizio

Convert HEX to DEC (and vice versa) Using Only Your Shell

Looking for a quick way to convert your hexadecimal piece to decimal? Other way around?

Just a simple

$ printf '%d' 0xCAFE # Output: 51966

will do.

And a DEC to HEX conversion is possible with

$ printf '%x' 51966 # Output: cafe

You can also use an inline expression with double parenthesis in order to get the decimal value and use it at your will

$ $((0xCAFE)) 
zsh: command not found: 51966

This will try to execute a command named 51966 which will generally duh fail..

Examples with printf assume that you have a POSIX Compliant Shell

You can read the man page for printf(1) to learn other printf options.