1849500 Members
6311 Online
104044 Solutions
New Discussion

Re: hex to decimal

 
SOLVED
Go to solution
Chris Frangandonis
Regular Advisor

hex to decimal

All,

I have 4 col's, the 4th col is in hex value (8e00). I would like to awk -printf $1-$4, with the 4th col being converted to dec.

Appreciate the assistance, Chris

5 REPLIES 5
Peter Kloetgen
Esteemed Contributor

Re: hex to decimal

Hi,

you could use the typeset- command to convert the format before giving it to awk- script:

typeset -i# var=value

# is for desired system:

typeset -i2 var=8
echo $var
2#100 --> binary system output

echo $var | cut -c3-
100 --> voila, the output you want, system is changable of course


Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Carlos Fernandez Riera
Honored Contributor
Solution

Re: hex to decimal

using ksh:


let -i h=16#1
while read a b c d
do
let h=16#$d
echo $a $b $c $d $h
done
unsupported
Chris Frangandonis
Regular Advisor

Re: hex to decimal

Hi Carlos,

Thanks, but when in hex (8e00), it should read as 008e, then 0x008e to be converted into dec.

Chris
Carlos Fernandez Riera
Honored Contributor

Re: hex to decimal

search 'hexadecimal' on the forum too.
unsupported
harry d brown jr
Honored Contributor

Re: hex to decimal

Chris,

Byte swapped eh?

try this:

echo 1234 2345 3456 8e00 | awk '
{
hexstr="0123456789abcdef"
newnum=tolower(substr($4,3,2) substr($4,1,2))
slen = length(newnum)
decnum = 0
for (i=1; i < slen+1;i++)
{
tpos = index(hexstr,substr(newnum,i,1)) - 1
decnum = decnum + ( (16 ** (slen -i)) * tpos)
}
printf("%s is %d\n",newnum,decnum)
} '



live free or die
harry
Live Free or Die