Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # numeric v1.6 - display positive decimal numbers as binary, octal & hexadecimal & their binary length.
- # You can use other numeric systems by entering numbers with the corresponding arithmetic expression,
- # i.e. $((0n)) for octal or $((0xn)) for hexadecimal.
- IN=$((${1##*[!0-9]*}+0)) # anything else but a decimal number is turned into 0
- DEC=0
- EXP=1
- while [ $IN -gt 0 ]; do # convert decimal to binary
- BIN=$((IN&1))$BIN
- : $((IN >>= 1))
- done
- [ ${#BIN} -lt 5 ] && LEN=5.${#BIN} || LEN=${#BIN}
- printf "%s %${LEN%.*}s\n" "BIN:" ${BIN:-0}
- while [ ${#BIN} -gt 0 ]; do # convert binary back to decimal
- DEC=$(($DEC+${BIN#${BIN%?}}*$EXP))
- EXP=$(($EXP*2))
- BIN=${BIN%?}
- done
- printf "%s %${LEN%.*}s\n" "OCT:" $(printf "%o" $DEC) "DEC:" $DEC "HEX:" $(printf "%x" $DEC) "LEN:" ${LEN#*.}" bit"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement