Created
January 4, 2017 02:44
-
-
Save ticchen/be96a832b07325539da2f486f567225a to your computer and use it in GitHub Desktop.
binary decimal hex coverter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################ | |
# generic | |
################################ | |
bindechex() | |
{ | |
if [ "$#" -le "1" ]; then | |
echo -e "Usage:\n\tbindechex <base:2/10/16> <val> ..." 2>/dev/null | |
return | |
fi | |
local base="$1" | |
shift | |
local hex="$@" | |
cat<<EOF | python3 - | |
val=int("$hex".replace(" ",""),$base); | |
print(" %s"%''.join( (str(int(x/10))+(" " if x%4==0 else "")) for x in range(31,-1,-1))); | |
print(" %s"%''.join( (str(x%10)+(" " if x%4==0 else "")) for x in range(31,-1,-1))); | |
print(" %s"%''.join( (str("-") +(" " if x%4==0 else "")) for x in range(31,-1,-1))); | |
print("bin=%s"%''.join( (list(reversed(bin(val)[2:].zfill(32)))[x] +(" " if (x)%4==0 else "")) for x in range(31,-1,-1))); | |
print("hex=0x%08x"%val); | |
print("dec=%d"%val); | |
#print("oct=%s"%oct(val).replace("o","")); | |
EOF | |
} | |
alias bdh=bindechex | |
################################ | |
# bin to X | |
################################ | |
bin2dec() | |
{ | |
bindechex 2 $@ | grep dec= | cut -d = -f 2 | |
} | |
alias b2d=bin2dec | |
bin2hex() | |
{ | |
bindechex 2 $@ | grep hex= | cut -d = -f 2 | |
} | |
alias b2h=bin2hex | |
################################ | |
# dec to X | |
################################ | |
dec2bin() | |
{ | |
bindechex 10 $@ | sed -n '1,/bin=/ p' | cut -c 5- | |
} | |
alias d2b=dec2bin | |
dec2hex() | |
{ | |
bindechex 10 $@ | grep hex= | cut -d = -f 2 | |
} | |
alias d2h=dec2hex | |
################################ | |
# hex to X | |
################################ | |
hex2bin() | |
{ | |
bindechex 16 $@ | sed -n '1,/bin=/ p' | cut -c 5- | |
} | |
alias h2b=hex2bin | |
hex2dec() | |
{ | |
bindechex 16 $@ | grep dec= | cut -d = -f 2 | |
} | |
alias h2d=hex2dec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment