#!/bin/sh # bcd.sh # Steve Parker, 16 April 2002 steve@steve-parker.org # Public Domain # Convert Decimal to BCD # eg: 9 becomes 1001 # 2 becomes 0010 # 92 becomes 1001 0010 convert_digit() { ####################################### ## it would be possible to do this ## ## mathematically, but since we will ## ## only ever have a single decimal ## ## digit, let's just use case ## ####################################### ####################################### ## depending on your OS and shell, ## ## getting echo to not use a newline ## ## character may require either of: ## ## echo "0101 \c" ## ## echo -en "0101 " ## ####################################### case $1 in 0) echo "0000 \c" ;; 1) echo "0001 \c" ;; 2) echo "0010 \c" ;; 3) echo "0011 \c" ;; 4) echo "0100 \c" ;; 5) echo "0101 \c" ;; 6) echo "0110 \c" ;; 7) echo "0111 \c" ;; 8) echo "1000 \c" ;; 9) echo "1001 \c" ;; *) echo echo "Invalid input $1, expected decimal digit" ;; esac } ################# ## main script ## ################# decimal=$1 stringlength=`echo $decimal | wc -c` char=1 while [ "${char}" -lt "${stringlength}" ] do convert_digit `echo $decimal|cut -c ${char}` char=`expr ${char} + 1` done echo