Skip to content

Instantly share code, notes, and snippets.

@ldante86
Created November 17, 2016 03:00
Show Gist options
  • Save ldante86/607696e81ef4b7a44146ca6d8958f80d to your computer and use it in GitHub Desktop.
Save ldante86/607696e81ef4b7a44146ca6d8958f80d to your computer and use it in GitHub Desktop.
native numeric conversions for zsh
#!/usr/bin/zsh
#
# PROGRAM: convert.zsh
# AUTHOR: Luciano D. Cecere
# DATE: 2013
script=$(basename $0)
typeset -i2 bin=$2
typeset -i8 oct=$2
typeset -i16 hex=$2
function bin {
if [ $bin = "2#0" ]; then
usage
else
printf "%s\n" "$(echo $bin | sed 's/2#//g')"
fi
}
function octal {
if [ $oct = "8#0" ]; then
usage
else
printf "%s\n" "$(echo $oct | sed 's/8#//g')"
fi
}
function hex {
if [ $hex = "16#0" ]; then
usage
else
printf "%s\n" "$(echo $hex | sed 's/16#//g')"
fi
}
function usage {
cat <<EOF
$script base conversion tool
usage:
./$script [-option] base-10-number
Options:
-b binary
-o octal
-h hexadecimal
EOF
exit 0
}
case $1 in
-b) bin ;;
-o) octal ;;
-h) hex ;;
*) usage
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment