Pejman Moghadam / Scripts

Bash script - base conversion

pibluc domain


hex2bin

#!/bin/bash
while :; do
    N=$RANDOM
    S=$(printf "%04X\n" $N)
    echo -n "0x${S}  "
    read
    for I in 0 1 2 3; do
            H=$(echo ${S:$I:1})
            B=$(echo "ibase=16;obase=2;$H" | bc)
            B=$(printf "%04d\n" $B)
            echo -n "$B "
    done
    echo; echo
done

dec2hexbin

#!/bin/bash
while :; do
    N=$(( $RANDOM / 256 ))
    echo "$N  "
    read
    S=$(printf "%04X\n" $N)
    echo "0x${S}  "
    for I in 0 1 2 3; do
            H=$(echo ${S:$I:1})
            B=$(echo "ibase=16;obase=2;$H" | bc)
            B=$(printf "%04d\n" $B)
            echo -n "$B "
    done
    echo; echo
done

BY: Pejman Moghadam
TAG: hex, dec, bin, bc, base, bash-script, bash
DATE: 2012-12-29 14:52:50


Pejman Moghadam / Scripts [ TXT ]