Skip to content

Instantly share code, notes, and snippets.

@ldante86
Created November 16, 2016 06:15
Show Gist options
  • Save ldante86/6eaf66ff75a9c2aa6f55962ed28087ee to your computer and use it in GitHub Desktop.
Save ldante86/6eaf66ff75a9c2aa6f55962ed28087ee to your computer and use it in GitHub Desktop.
convert decimal to hexadecimal
#!/bin/bash -
#
# SCRIPT: hexnote
# AUTHOR: Luciano D. Cecere
########################################################################
#
# hexnote - convert base 10 integers to hexadecimal notation
# Copyright (C) 2015 Luciano D. Cecere <ldante86@aol.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
########################################################################
export PATH=/bin:/usr/bin
unalias -a
PROGRAM="${0##*/}"
base16=( {0..10} {A..F} )
_main()
{
dec="$1" digit= size= hex=
while [ $dec -ne 0 ]
do
((digit=dec % 16))
if [ $digit -ge 10 ]; then
hex=${base16[digit+1]}${hex}
else
hex=${base16[digit]}${hex}
fi
((dec/=16))
done
echo ${hex}
}
[ $# -eq 0 ] && echo "Usage: $PROGRAM [-l] numbers"
while [ $# -ne 0 ]
do
case $1 in
-l)
base16=( {0..10} {a..f} )
;;
0)
echo 0
;;
*.*|*[a-z]*|*[A-Z]*|-*|0*)
true
;;
*)
_main "$1"
;;
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment