1830993 Members
2483 Online
110018 Solutions
New Discussion

reverse the bytes

 
SOLVED
Go to solution
Chris Frangandonis
Regular Advisor

reverse the bytes

Hi All,

How could I reverse the bytes for eg: 8e00 (hex) in the 4th col to read 008e then convert it to dec for eg:

let -i h=16#1
while read a b c d
do
let h=16#$d
echo $a $b $c $d $h
done
4 REPLIES 4
Robin Wakefield
Honored Contributor

Re: reverse the bytes

Hi,

Try:

# d1=008e
# d2=`echo $d1 | sed 's/\(..\)\(..\)/\2\1/'`
# echo $d2
8e00

Rgds, Robin.
Carlos Fernandez Riera
Honored Contributor
Solution

Re: reverse the bytes

#/usr/bin/ksh
let h=16#1
while read a b c d
do
typeset -L2 b1=$d
typeset -R2 b2=$d
let h=16#$b2$b1
echo $a $b $c $d $h
done
unsupported
harry d brown jr
Honored Contributor

Re: reverse the bytes

chris,

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)
} '

note the "substr" area where "newnum" is assigned

live free or die
harry
Live Free or Die
Chris Frangandonis
Regular Advisor

Re: reverse the bytes

Hi Carlos,Harry

Thanks for all you help.

Chris