- - - - - As of February 1st 2014 this blog is no longer updated - - - - -
Convert numeral systems (dec <-> bin <-> hex <-> oct)
Tags binary, convert, decimal, hexadecimal, octal
Converting from one numeral system to another can be done using the bc command.
Here are a few examples in the form of functions:
# -------------------------------------------------------------------------- #
# - Decimal to ....
# -------------------------------------------------------------------------- #
# -------------------------------------------------------------------------- #
# - Hexadecimal to ...
# -------------------------------------------------------------------------- #
The above is based on bash4, if an older bash version is used change: in=${1^^}
to: in=$( echo $1 | tr '[:lower:]' '[:upper:]' )
# -------------------------------------------------------------------------- #
# - Binary to ...
# -------------------------------------------------------------------------- #
# -------------------------------------------------------------------------- #
# - Octal to ...
# -------------------------------------------------------------------------- #
Here are a few examples in the form of functions:
# -------------------------------------------------------------------------- #
# - Decimal to ....
# -------------------------------------------------------------------------- #
- Binary
PHP Code:
# Convert decimal to binary
function decToBin() {
echo "obase=2;$1" | bc
}
Code:
$ decToBin 19 10011
- Hexadecimal
PHP Code:
# Convert decimal to hexadecimal
function decToHex() {
echo "obase=16; $1" | bc
}
Code:
$ decToHex 19 13
- Octal
PHP Code:
# Convert decimal to octal
function decToOct() {
echo "obase=8;$1" | bc
}
Code:
$ decToOct 19 23
# - Hexadecimal to ...
# -------------------------------------------------------------------------- #
- Binary
PHP Code:
# Convert hexadecimal to binary
function hexToBin() {
in=${1^^}
echo "ibase=16;obase=2;$in" | BC_LINE_LENGTH=0 bc
}
Code:
$ hexToBin 13 10011
- Decimal
PHP Code:
# Convert hexadecimal to decimal
function hexToDec() {
in=${1^^}
echo "ibase=16;$in" | bc
}
Code:
$ hexToDec 13 19
- Octal
PHP Code:
# Convert hexadecimal to octal
function hexToOct() {
in=${1^^}
echo "ibase=16;obase=8;$in" | bc
}
Code:
$ hexToOct 13 23
to: in=$( echo $1 | tr '[:lower:]' '[:upper:]' )
# -------------------------------------------------------------------------- #
# - Binary to ...
# -------------------------------------------------------------------------- #
- Decimal
PHP Code:
# Convert binary to decimal
function binToDec() {
echo "ibase=2;$1" | bc
}
Code:
$ binToDec 10011 19
- Hexadecimal
PHP Code:
# Convert binary to hexadecimal
function binToHex() {
echo "ibase=2;obase=10000;$1" | bc
}
Code:
$ binToHex 10011 13
- Octal
PHP Code:
# Convert binary to octal
function binToOct() {
echo "ibase=2;obase=1000;$1" | bc
}
Code:
$ binToOct 10011 23
# - Octal to ...
# -------------------------------------------------------------------------- #
- Binary
PHP Code:
# Convert octal to binary
function octToBin() {
echo "ibase=8;obase=2;$1" | bc
}
Code:
$ octToBin 23 10011
- Decimal
PHP Code:
# Convert octal to decimal
function octToDec() {
echo "ibase=8;obase=12;$1" | bc
}
Code:
$ octToDec 23 19
- Hexadecimal
PHP Code:
# Convert octal to hexadecimal
function octToHex() {
echo "ibase=8;obase=20;$1" | bc
}
Code:
$ octToHex 23 13