home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1995 November
/
PCWK1195.iso
/
inne
/
podstawy
/
dos
/
4dos
/
4uzytki
/
4dtnt.exe
/
STRING.ZIP
/
TODEC.BTM
< prev
next >
Wrap
Text File
|
1991-11-03
|
3KB
|
103 lines
Convert an operand (VALUE of an environment variable whose NAME is
given) to decimal fom the specified number base. See BUMP for
more details
if "%1" == "" .or. "%2" == "" .or. "%[%2]" == "" goto help
set $foo=%@upper[%1]
set $wstr=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
If the base is given by mnemonic letter, convert it to the
appropriate number.
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
Validate the base of the number system and call the conversion if OK.
iff %$foo gt 1 .and. %$foo lt 37 then ^ gosub base
else goto help
endiff
Check the result of conversion and return it if valid.
iff %$val ne 0 then^unset %2 ^ (gosub cleanup^goto helpval)
else set %2=%$foo ^gosub cleanup^quit 0
endiff
quit
:cleanup
unset $foo $wstr $val $max >& nul
return
:base
set $max=%$foo
gosub cnvtit
return
The heart of the batch -- the actual validation and conversion.
:cnvtit
Set all input characters to upper case to standardize
set $foo=%@upper[%[%2]]
Limit the length of the loop to the length of the string
set $i=%@eval[%@len[%$foo]-1]
Set conversion multiplier to 1 for rightmost digit
set $mult=1
Initialize value accumulator
set $accum=0
:cnvtloop
The base value of the current digit is its offset into wstr. If
that value is greater than the number system base or -1 (no such
character) drop dead because of the invalid character.
set $val=%@index[%$wstr,%@substr[%[%2],%$i,1]]
if %$val lt 0 .or. %$val ge %$max (set $val=%$max^goto cnvtexit)
Multiply the base value by the multiplier and add to the accumulator.
set $accum=%@eval[%$accum+%@eval[%$val*%$mult]]
Change the multiplier to the value of digits in the next left column.
set $mult=%@eval[%$mult*%$max]
Point at the next left character
set $i=%@eval[%$i-1]
See if we're done.
if %$i ge 0 goto cnvtloop
Return the accumulated total.
set $foo=%$accum
Set OK result indication.
set $val=0
:cnvtexit
unset $i $mult $accum >& 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