Operating System - HP-UX
1822549 Members
2879 Online
109642 Solutions
New Discussion юеВ

Working with HEX in scripts

 
SOLVED
Go to solution
Mike Rightmire
Frequent Advisor

Working with HEX in scripts

Hey!
I am trying to write a simple script which determines a free node number for use with the mknode command. The real question is, how do I work with hex numbers in shell scripts there must be a simply way (other than writing a conversion in the script.)

Very simply I need to count in hex and not decimal. I need to be able to add 5 + 5 and come up with A (not ten) and add another 5 to it and come up with F, ETC.

Thanks for the help. I am sure this is a simple question with a simple command, but I don't know what it is :-)

Mike
"If we treated each person we met as if they were carrying an unspeakable burden, we might almost treat each other as we should." Dale Carnegie
7 REPLIES 7
Patrick Wallek
Honored Contributor

Re: Working with HEX in scripts

Have a look at 'dc'. I don't know if this'll do what you want or not, but here's the man page on it.

http://docs.hp.com/hpux/onlinedocs/B2355-90680/B2355-90680.html
A. Clay Stephenson
Acclaimed Contributor

Re: Working with HEX in scripts

Hi Mike:

My utility of choice would be bc.

e.g.
bc
obase=16 # sets output base to hex
5 + 5 # add 5 decimal + 5 decimal
A
quit

You will have problems doing a running total because A + 5 in decimal ibase is an error; you could do all input in hex to solve that by specifying ibase=16 as well.

The other simple answer is to create a small script to simply do decimal to hex conversion:

#!/usr/bin/sh

while [ $# -ge 1 ]
do
echo "obase=16;${1};quit" | bc
shift
done


you would then execute my.sh 10 20
to output:
A
14

Another variation:

X="0"
while [ -n "${X}" ]
do
echo "Enter decimal value ( to quit): \c"
read X
if [ -n "${X}" ]
then
echo "Hex: \c"
echo "obase=16;${X};quit" | bc
fi
done

Man bc for details.

Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Working with HEX in scripts

Hi Mike:

Another way is to do your arithmetic in decimal and let the 'printf' function convert decimal to hexadecimal, as:

#!/usr/bin/sh
let N=5+5+5
printf "%#s %#x\n" "N =" $N

Regards!

...JRF...
linuxfan
Honored Contributor

Re: Working with HEX in scripts

Hi Mike,


You could try something like this

MINOR=$(ls -l /dev/*/group | awk ' {print $6}' | sed "s/0x//;s/0000$//" | sort |tail -1)

MINOR=$(echo "obase=16;$MINOR+1;quit"|bc)

MINOR=0x0${MINOR}0000

echo "The new minor number is $MINOR"

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Carlos Fernandez Riera
Honored Contributor

Re: Working with HEX in scripts

For ksh

#! /usr/bin/ksh

let dec=16#1F
echo $dec
printf "%d , %x \n" $dec $dec



Am i a wizard or not?
unsupported
Robin Wakefield
Honored Contributor

Re: Working with HEX in scripts

Hi Mike,

...and using let/typeset:

================================
#!/bin/ksh
typeset -i16 z
while [ $# -ne 0 ] ; do
let z=z+16#$1
shift
done
echo $z | cut -d# -f2
================================

# ./myscript.ksh e 5 a
1d

Rgds, Robin
Mike Rightmire
Frequent Advisor

Re: Working with HEX in scripts

Hey Gang!

Thanks for all the help. The final script was too long to place here, but it ended up being a combination of James' printf commands and Ramesh's script.

Thanks again for all the help!
Mike
"If we treated each person we met as if they were carrying an unspeakable burden, we might almost treat each other as we should." Dale Carnegie