Shell Script – Binary to Hexadecimal and Decimal Conversion

arithmeticbinaryshell-script

I have a context where I need to convert binary to hexadecimal and decimal and viceversa in a shell script. Can someone suggest me a tool for this?

Best Answer

It's fairly straightforward to do the conversion from binary in pure bash (echo and printf are builtins):

Binary to decimal

$ echo "$((2#101010101))"
341

Binary to hexadecimal

$ printf '%x\n' "$((2#101010101))"
155

Going back to binary using bash alone is somewhat more complex, so I suggest you see the other answers for solutions to that.

Related Question