[Bash] Numeral Systems Conversion

As a computer programmer, you must to know about the different numeral systems. In your works, there will be lots of times that you’ll be using numeral systems like the binary, hexadecimal… In this post, we’ll see some useful functions in Bash that can do some conversions.

We’ll discove Binary, Octal, Decimal and Hexadecimal and ASCII also, the best way to use those functions is to paste the source code in your .bashrc file in home directory.

Binaries Converter

function bin2all() {
    if [[ $1 ]]; then
        echo "Binary $1 = Octal $(bin2oct $1)"
        echo "Binary $1 = Decimal $(bin2dec $1)"
        echo "Binary $1 = Hexadecimal $(bin2hex $1)"
        echo "Binary $1 = Base32 $(bin2base32 $1)"
        echo "Binary $1 = Base64 $(bin2base64 $1)"
        echo "Binary $1 = ASCII $(bin2ascii $1)"
    else
        echo "Convert Binary numbers to all representations"
        echo "Usage: bin2all [Binary numbers]"
    fi
}

function bin2oct() {
    if [[ $1 ]]; then
        echo "obase=8 ; ibase=2 ; $1" | bc
    else
        echo "Convert Binary numbers to Octal"
        echo "Usage: bin2oct [Binary numbers]"
    fi
}

function bin2dec() {
    if [[ $1 ]]; then
        echo $((2#$1))
    else
        echo "Convert Binary numbers to Decimal"
        echo "Usage: bin2dec [Binary numbers]"
    fi
}

function bin2hex() {
    if [[ $1 ]]; then
        echo "obase=16 ; ibase=2 ; $1" | bc
    else
        echo "Convert Binary numbers to Hexadecimal"
        echo "Usage: bin2hex [Binary numbers]"
    fi
}

function bin2base32() {
    if [[ $1 ]]; then
        echo "obase=32 ; ibase=2 ; $1 " | bc
    else
        echo "Convert Binary numbers to Base32"
        echo "Usage: bin2base32 [Binary numbers]"
    fi
}

function bin2base64() {
    if [[ $1 ]]; then
        echo "obase=64 ; ibase=2 ; $1" | bc
    else
        echo "Convert Binary numbers to Base64"
        echo "Usage: bin2base64 [Binary numbers]"
    fi
}

function bin2ascii() {
    if [[ $1 ]]; then
        echo -e "\0$(printf %o $((2#$1)))"
    else
        echo "Convert Binary numbers to ASCII characters"
        echo "Usage: bin2acii [Binary numbers]"
    fi
}

Octals Converter

function oct2all() {
    if [[ $1 ]]; then
        echo "Octal $1 = Binary $(oct2bin $1)"
        echo "Octal $1 = Decimal $(oct2dec $1)"
        echo "Octal $1 = Hexadecimal $(oct2hex $1)"
        echo "Octal $1 = Base32 $(oct2base32 $1)"
        echo "Octal $1 = Base64 $(oct2base64 $1)"
        echo "Octal $1 = ASCII $(oct2ascii $1)"
    else
        echo "Convert Octal numbers to all representations"
        echo "Usage: oct2all [Octal numbers]"
    fi
}

function oct2bin() {
    if [[ $1 ]]; then
        echo "obase=2 ; ibase=8 ; $1" | bc
    else
        echo "Convert Octal numbers to Binary"
        echo "Usage: oct2bin [Octal numbers]"
    fi
}

function oct2dec() {
    if [[ $1 ]]; then
        echo $((8#$1))
    else
        echo "Convert Octal numbers to Decimal"
        echo "Usage: oct2dec [Octal numbers]"
    fi
}

function oct2hex() {
    if [[ $1 ]]; then
        echo "obase=16 ; ibase=8 ; $1" | bc
    else
        echo "Convert Octal numbers to Hexadecimal"
        echo "Usage: oct2hex [Octal numbers]"
    fi
}

function oct2base32() {
    if [[ $1 ]]; then
        echo "obase=32 ; ibase=8 ; $1" | bc
    else
        echo "Convert Octal numbers to Base32"
        echo "Usage: oct2base32 [Octal numbers]"
    fi
}

function oct2base64() {
    if [[ $1 ]]; then
        echo "obase=64 ; ibase=8 ; $1" | bc
    else
        echo "Convert Octal numbers to Base64"
        echo "Usage: oct2base64 [Octal numbers]"
    fi
}

function oct2ascii() {
    if [[ $1 ]]; then
        echo -e "\0$(printf %o $((8#$1)))"
    else
        echo "Convert Octal numbers to ASCII characters"
        echo "Usage: oct2ascii [Octal numbers]"
    fi
}

Decimals Converter

function dec2all() {
    if [[ $1 ]]; then
        echo "Decimal $1 = Binary $(dec2bin $1)"
        echo "Decimal $1 = Octal $(dec2oct $1)"
        echo "Decimal $1 = Hexadecimal $(dec2hex $1)"
        echo "Decimal $1 = Base32 $(dec2base32 $1)"
        echo "Decimal $1 = Base64 $(dec2base64 $1)"
        echo "Decimal $1 = ASCII $(dec2ascii $1)"
    else
        echo "Convert Decimal numbers to all representations"
        echo "Usage: dec2all [Decimal numbers]"
    fi
}

function dec2bin() {
    if [[ $1 ]]; then
        echo "obase=2 ; $1" | bc
    else
        echo "Convert Decimal numbers to Binary"
        echo "Usage: dec2bin [Decimal numbers]"
    fi
}

function dec2oct() {
    if [[ $1 ]]; then
        echo "obase=8 ; $1" | bc
    else
        echo "Convert Decimal numbers to Octal"
        echo "Usage: dec2oct [Decimal numbers]"
    fi
}

function dec2hex() {
    if [[ $1 ]]; then
        echo "obase=16 ; $1" | bc
    else
        echo "Convert Decimal numbers to Hexadecimal"
        echo "Usage: dec2hex [Decimal numbers]"
    fi
}

function dec2base32() {
    if [[ $1 ]]; then
        echo "obase=32 ; $1" | bc
    else
        echo "Convert Decimal numbers to Base32"
        echo "Usage: dec2base32 [Decimal numbers]"
    fi
}

function dec2base64() {
    if [[ $1 ]]; then
        echo "obase=64 ; $1" | bc
    else
        echo "Convert Decimal numbers to Base64"
        echo "Usage: dec2base64 [Decimal numbers]"
    fi
}

function dec2ascii() {
    if [[ $1 ]]; then
        echo -e "\0$(printf %o 97)"
    else
        echo "Convert Decimal numbers to ASCI characters"
        echo "Usage: dec2ascii [Decimal numbers]"
    fi
}

Hexadecimals Converter

function hex2all() {
    if [[ $1 ]]; then
        echo "Hexadecimal $1 = Binary $(hex2bin $1)"
        echo "Hexadecimal $1 = Octal $(hex2oct $1)"
        echo "Hexadecimal $1 = Decimal $(hex2dec $1)"
        echo "Hexadecimal $1 = Base32 $(hex2base32 $1)"
        echo "Hexadecimal $1 = Base64 $(hex2base64 $1)"
        echo "Hexadecimal $1 = ASCII $(hex2ascii $1)"
    else
        echo "Convert Hexadecimal numbers to all representations"
        echo "Usage: hex2all [Hexadecimal numbers]"
    fi
}

function hex2bin() {
    if [[ $1 ]]; then
        echo "obase=2 ; ibase=16 ; $1" | bc
    else
        echo "Convert Hexadecimal numbers to Binary"
        echo "Usage: hex2bin [Hexadecimal numbers]"
    fi
}

function hex2oct() {
    if [[ $1 ]]; then
        echo "obase=8 ; ibase=16 ; $1" | bc
    else
        echo "Convert Hexadecimal numbers to Octal"
        echo "Usage: hex2oct [Hexadecimal numbers]"
    fi
}

function hex2dec() {
    if [[ $1 ]]; then
         echo $((16#$1))
    else
        echo "Convert Hexadecimal numbers to Decimal"
        echo "Usage: hex2dec [Hexadecimal numbers]"
    fi
}
function hex2base32() {
    if [[ $1 ]]; then
        echo "obase=32 ; ibase=16 ; $1" | bc
    else
        echo "Convert Hexadecimal numbers to Base32"
        echo "Usage: hex2base32 [Hexadecimal numbers]"
    fi
}

function hex2base64() {
    if [[ $1 ]]; then
        echo "obase=64 ; ibase=16 ; $1" | bc
    else
        echo "Convert Hexadecimal numbers to Base64"
        echo "Usage: hex2base64 [Hexadecimal numbers]"
    fi
}

function hex2ascii() {
    if [[ $1 ]]; then
        echo -e "\0$(printf %o $((16#$1)))"
    else
        echo "Convert Hexadecimal numbers to ASCII characters"
        echo "Usage: hex2ascii [Hexadecimal numbers]"
    fi
}

ASCII Converter

function ascii2all() {
    if [[ $1 ]]; then
        echo "ASCII $1 = Binary $(ascii2bin $1)"
        echo "ASCII $1 = Octal $(ascii2oct $1)"
        echo "ASCII $1 = Decimal $(ascii2dec $1)"
        echo "ASCII $1 = Hexadecimal $(ascii2hex $1)"
        echo "ASCII $1 = Base32 $(ascii2base32 $1)"
        echo "ASCII $1 = Base64 $(ascii2base64 $1)"
    else
        echo "Convert ASCII characters to all representations"
        echo "Usage: ascii2all [ASCII characters]"
    fi
}

function ascii2bin() {
    if [[ $1 ]]; then
        echo "obase=2 ; $(ascii2dec $1)" | bc
    else
        echo "Convert ASCII characters to Binary"
        echo "Usage: ascii2bin [ASCII characters]"
    fi
}

function ascii2oct() {
    if [[ $1 ]]; then
        echo "obase=8 ; $(ascii2dec $1)" | bc
    else
        echo "Convert ASCII characters to Octal"
        echo "Usage: ascii2oct [ASCII characters]"
    fi
}

function ascii2dec() {
    if [[ $1 ]]; then
        printf '%d\n' "'$1'"
    else
        echo "Convert ASCII characters to Decimal"
        echo "Usage: ascii2dec [ASCII characters]"
    fi
}

function ascii2hex() {
    if [[ $1 ]]; then
        echo "obase=16 ; $(ascii2dec $1)" | bc
    else
        echo "Convert ASCII characters to Hexadecimal"
        echo "Usage: ascii2hex [ASCII characters]"
    fi
}

function ascii2base32() {
    if [[ $1 ]]; then
        echo "obase=32 ; $(ascii2dec $1)" | bc
    else
        echo "Convert ASCII characters to Base32"
        echo "Usage: ascii2base32 [ASCII characters]"
    fi
}

function ascii2base64() {
    if [[ $1 ]]; then
        echo "obase=64 ; $(ascii2dec $1)" | bc
    else
        echo "Convert ASCII characters to Base64"
        echo "Usage: ascii2base64 [ASCII characters]"
    fi
}

Well, that’s my take on it, anyway. What do you guys think ?

Advertisements

Tagged: , , , , , , , ,

One thought on “[Bash] Numeral Systems Conversion

  1. Rod Smith March 3, 2014 at 9:00 pm Reply

    In function dec2ascii(), there’s a typo — the 97 should be $1, I think.

Leave a Reply :

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: