A better @convert

samintz

Scott Mintz
May 20, 2008
1,295
11
Solon, OH, USA
#1
The built-in @convert limits values to 64 bits. However, @eval can go up to 10000 digits. I wrote the following script as an alternative.

Code:
setlocal
if %1 == %2 (echo %3 & quit)
if %1 == 10 (call :BaseX %3 %2 & quit)
if %2 == 10 (call :Base10 %3 %1 & quit)
Call :BaseX %@execstr[call :Base10 %3 %1] %2
quit

:BaseX 
set b10=%1
set result=
do until %b10==0
    set rmndr=%@eval[((%b10 / %2) - %@INT[%b10 / %2]) * %2]
    if %rmndr GT 9 set rmndr=%@char[%@eval[%rmndr + 55]]
    set result=%[rmndr]%[result]
    set b10=%@int[%b10 / %2]
enddo
if %result.==. set result=0
echo %result
quit

:Base10
set b2=%1
set result=0
do until %b2.==.
    set digit=%@left[1,%b2]
    iff %@ascii[%digit] GT 57 then 
        set digit=%@eval[(%@ascii[%digit] OR 32) - 87]
    endiff
    set result=%@eval[(%result * %2) + %digit]
    set b2=%@right[-1,%b2]
enddo
echo %result
quit

endlocal
Here is the function:
Code:
function cvt=`%@execstr[convert.btm %1 %2 %3]`
And here are some examples:
Code:
echo %@cvt[10,12,%@eval[13000 * 33333 * 31416 * 27813 * 98765 * 43210]]
183AB5A45118A2082453522000
echo %@cvt[12,10,183AB5A45118A2082453522000]
1615860307974787525450800000
-Scott