home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1995 November
/
PCWK1195.iso
/
inne
/
podstawy
/
dos
/
4dos
/
4uzytki
/
4dtnt.exe
/
STRING.ZIP
/
FROMDEC.DOC
< prev
next >
Wrap
Text File
|
1991-11-03
|
3KB
|
112 lines
Convert from decimal to the caller's specified system base. This is
an evolutionary step from BUMP and from TODEC.
if "%1" == "" .or. "%2" == "" .or. "%[%2]" == "" goto help
set $foo=%@upper[%1]
set $wstr=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
Validate the input variable as being a valid decimal number.
gosub valdec
if %$val gt 9 (gosub cleanup ^ goto helpval)
Validate the target number base and set the parameters.
iff %$foo = B then ^ set $foo=2
elseiff %$foo = O then ^ set $foo=8
elseiff %$foo = H then ^ set $foo=16
elseiff %$foo = D then ^ set $foo=10
endiff
iff %$foo gt 1 .and. %$foo lt 37 then ^ gosub base
else goto help
endiff
Return value to caller.
set %2=%$foo ^gosub cleanup^quit 0
:cleanup
unset $foo $wstr $val $base >& nul
return
:base
set $base=%$foo
gosub cnvtit
return
The "meat" routine.
:cnvtit
Set a divisor equal to the target number base.
set $div=%$foo
Standardizing the case is actually irrelevant in decimal, but this IS
a carryover from BUMP, etc. where it makes sense.
set $num=%@upper[%[%2]]
This step corresponds to initializing the accumulator in TODEC and isn't
really functional here.
set $temp=0
Initialize the output string.
set $foo=
:cnvtloop
Get the remainder (modulo) to get the value of this (rightmost) digit.
set $temp=%@eval[%$num %% %$base]
Use the remainder as an index into the 36 character string to get the
correct digit character and prepend it to the left of the output
string.
set $foo=%@substr[%$wstr,%$temp,1]%$foo
Sleaze off the remainder prior to the next division. This could be
handled more elegantly by @int, but @int was added to 4DOS long after
this was written.
set $num=%@eval[%$num - %$temp]
Divide our way down to the next digit to the left
set $num=%@eval[%$num / %$base]
See if we've exhausted the input value.
if %$num gt 0 goto cnvtloop
:cnvtexit
unset $div $num $temp >& nul
return
:helpval
echo Invalid character detected.
echo .
goto help
:help
echo Usage: %@name[%0] mode var
echo.
echo var = Any environment variable whose value is the input
echo mode = any of:
echo B=binary [0-1]
echo D=decimal [0-9]
echo H=hex [0-F]
echo O=octal [0-7]
echo [2-36] [0-(n-1)]
echo New value returned in var, replacing original value
quit 4
See whether a string is nothing but digits 0 - 9 by testing each
character of the input string for a valid position in wstr.
:valdec
set $i=0
:valdoop
set $val=%@index[%$wstr,%@substr[%[%2],%$i,1]]
if %$val lt 0 .or. %$val gt 9 (set $val=99 ^ goto valdexit)
set $i=%@eval[%$i+1]
if %$i lt %@len[%$foo] goto valdoop
:valdexit
unset $i >& NUL
return