Operating System - HP-UX
1820475 Members
3337 Online
109624 Solutions
New Discussion юеВ

simple calulation script...

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

simple calulation script...

simple for most of you guys at least! ;)

I've got a number that is translated from a numberical string as follows:

numberical string: 1.1.100
(max 255.255.255)
256^2 * X + 256^1 * Y + Z

ie 1.1.100 becomes
65892

How can I go backwards in a script?

Thanks,
Bill
It works for me (tm)
4 REPLIES 4
Santosh Nair_1
Honored Contributor

Re: simple calulation script...

How about something like this (needs some cleanups, like prompts, error checking, etc):

----------cut here---------
#!/bin/sh

read input
OLDIFS=$IFS
IFS=.
set -- $input
echo $1 $2 $3
let sum=$1*256+$2
let sum=sum*256+$3
echo $sum
IFS=$OLDIFS
----------cut here---------

-Santosh
Life is what's happening while you're busy making other plans
Carlos Fernandez Riera
Honored Contributor

Re: simple calulation script...


echo $*
echo $1 | awk -F. ' { print $1*256^2 + $2*256 +$3 }'
unsupported
Robin Wakefield
Honored Contributor
Solution

Re: simple calulation script...

Hi Bill,

If you mean convert a number back to x.y.z, then create a file containing:

{ for (i=3;i>=1;i--)
{ val[i]=256*($0/256-int($0/256))
$0=int($0/256)
}
printf ("%d.%d.%d\n",val[1],val[2],val[3])
}

echo 965892|awk -f/tmp/conv.awk
14.189.4

Rgds, Robin.
Volker Borowski
Honored Contributor

Re: simple calulation script...

Many ways to achieve:
How about this one ?

# V=65892
# echo `expr $V / 65536`.`expr $V % 65536 / 256`.`expr $V % 256`
1.1.100
#

Volker