Number base conversions on the command line

tags: aliases  bash  perl 

I made some bash aliases to convert between number bases. Remember that Perl’s oct can translate the other bases as long as the input string has the prefix, such as 0b10101:

alias d2h="perl -e 'printf qq|%X\n|, int( shift )'"
alias d2o="perl -e 'printf qq|%o\n|, int( shift )'"
alias d2b="perl -e 'printf qq|%b\n|, int( shift )'"
alias h2d="perl -e 'printf qq|%d\n|, hex( shift )'"
alias h2o="perl -e 'printf qq|%o\n|, hex( shift )'"
alias h2b="perl -e 'printf qq|%b\n|, hex( shift )'"
alias o2h="perl -e 'printf qq|%X\n|, oct( shift )'"
alias o2d="perl -e 'printf qq|%d\n|, oct( shift )'"
alias o2b="perl -e 'printf qq|%b\n|, oct( shift )'"
alias b2h="perl -e 'printf qq|%X\n|, oct( q(0b) . shift )'"
alias b2d="perl -e 'printf qq|%d\n|, oct( q(0b) . shift )'"
alias b2o="perl -e 'printf qq|%o\n|, oct( q(0b) . shift )'"

Here are some runs:

$ d2h 137
$ h2d 89
$ d2b 137

I’ve had these forever and first posted them on a journal in 2008 (the binary ones were a comment from another user). I mention them in my classes and people want a place to copy them, so here they are again. The aliases tag has more of these.

At various times I’ve tried to get rid of the perl, but I haven’t found another tool (that’s not another programming language) that can easily handle all the bases and easily take arguments. You might like to read:

Somehow the Perl solutions in those found pack instead of the easier tools such as hex designed for this.