Greetings
I knew as soon as I saw the first post on this that I would have to
write the script to convert any number to any base, and, finally friday
I found the time (our main application uses lots of bitmaps, so, as an
excuse, I decided it could come in handy while error-tracking...).
It's not perfect, and not optimized, but it seems to work for positive
numbers (and bases less than 36) on our 5.5 system. I also used a
help-script (to do 'character-based' division to avoid
integer-overflow). Comments welcome...
in this particular case (if you hadn't already a working solution) it
could be used as:
: setvar timestamp, "!hpyyyy" + rht("0"+"!hpmonth",2) + &
rht("0"+"!hpdate",2) + rht("0" + "!hphour",2) + &
rht("0" + "!hpminute",2)
: conv !timestamp,36,ftpfile,false
giving the converted number in 'ftpfile'
/per (files below)
--------------------------------------------
Per Ostberg / [log in to unmask]
phone +46-8-58581213 / fax +46-8-58587710
Huddinge University Hospital
ChemLab / C1 74 / S-141 86 HUDDINGE / SWEDEN
--------------------------------------------
main script:
:help conv
USER DEFINED COMMAND FILE: CONV.PUB.COMMANDS
--8<-------------
parm num,base=2,returnvar="none",visualize=hpInteractive
# convert number (positive, in decimal) to any base between 1
# and 36 (inclusive)
# parms: num - number to convert
# base - base to use
# returnvar - set this ci-var to the converted number
# visualize - (if true) insert spaces for easier reading
# (according to my preferences, wich are:
# binary - groups of four,
# oktal - groups of three
# hex - groups of two)
#
# when what who
# 20000707 Creation per ([log in to unmask])
# --------------------------------------------------------------------
# number to convert, check:
setvar _cnum,"!num"
if not numeric(_cnum)
echo Number to convert must be in decimal notation!
return
endif
#check base:
if !base <= 0 or !base > 36
echo Bad base.... (1..36)
return
elseif !base=1
# ;-)
echo ![rpt("1",!num)]
return
endif
# digits to use:
setvar _cdigits "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# separate every _csep char (if visualize)
if !base < 8
setvar _csep,4
elseif !base < 16
setvar _csep,3
else
setvar _csep,2
endif
setvar _cloop,0
# init output:
setvar _cres ""
setvar _cdone false
# quiet mode...
setvar _coldfence,hpMsgFence
setvar hpMsgfence,2
# here we go:
while not _cdone
errclear
continue
setvar _cdigpos, (!_cnum mod !base)+1
if cierror <> 9740
# no integer overflow:
# add to result-string
setvar _cres, str(_cdigits,_cdigpos,1) + _cres
setvar _cnum,!_cnum/!base
else
# Integer overflow in the expression. (CIERR 9740)
# perform arithmetic:
xdiv "!_cnum",!base,"_cnum","_cdigpos"
# add to result-string
setvar _cres, str(_cdigits,_cdigpos + 1,1) + _cres
endif
if !visualize
# insert spaces for readability:
if setvar(_cloop,_cloop + 1) mod _csep = 0
setvar _cres, " " + _cres
endif
endif
# better too many terminating-conditions, than to few...
setvar _cdone,"!_cnum"="" or "!_cnum"="0"
endwhile
# back to verbose mode
setvar hpmsgfence,_coldfence
if "!returnvar" <> "none"
setvar !returnvar _cres
endif
echo !_cres
if not bound(tracevar)
deletevar _c@
endif
---------division-script:
:help xdiv
USER DEFINED COMMAND FILE: XDIV.PUB.COMMANDS
--8<-------------
parm number,divisor,Quotient_var,Remainder_var
# character-based integer-division:
# divide number by divisor; put result in Quotient_var,
# and remainder in Remainder_var
#
# when what who
# 20000707 Creation per ([log in to unmask])
setvar !Quotient_var ""
setvar _sd_pos 0
setvar _sd_quot 0
while setvar(_sd_pos,_sd_pos + 1) <= len("!number")
setvar _sd_quot,_sd_quot*10 + ![str("!number",_sd_pos,1)]
setvar _sd_num, _sd_quot / !divisor
if _sd_num > 0 or len(!Quotient_var) > 0
setvar !Quotient_var, !Quotient_var + "!_sd_num"
endif
setvar _sd_quot, _sd_quot mod !divisor
endwhile
setvar !Remainder_var,_sd_quot
if not bound(tracevar)
deletevar _sd_@
endif
--------------------------
> Thanks to everyone's help and suggestions, here is what I came up with
(MPE/iX 5
> .5 pp7):
>
>
> !# Based on an algorithm suggested by Jeff Woods [log in to unmask]
> !# with a modification suggested by Rollie Amurao
[log in to unmask]
> !# for limiting the year portion of the filename
> !# and with thanks for the rest of the HP3000 mailing list for their
..........
|