Rem Project: value2base_v0.1
Rem Created: Tuesday, July 06, 2021
Rem ***** Main Source File *****
sync on : sync 30 : sync
decval=255
base=2
binary$=value2base(decval,2)
octal$=value2base(decval,base+6)
hexdec$=value2base(decval,base+14)
print str$(decval);" -> ";binary$;" (binary)"
print str$(decval);" -> ";octal$;" (octal)"
print str$(decval);" -> ";hexdec$;" (hexadecimal)"
sync
wait key
end
function value2base(v,base)
local b$, t$, rmd, v2
repeat
rmd = v mod base
v2 = v / base
`print v; ", ";v2; ", ";rmd;", base= ";base // debug only
v = v2
if base = 16
if rmd >=10 and rmd<=15
if rmd=10 then t$="A"
if rmd=11 then t$="B"
if rmd=12 then t$="C"
if rmd=13 then t$="D"
if rmd=14 then t$="E"
if rmd=15 then t$="F"
else
t$=str$(rmd)
endif
else
t$=str$(rmd)
endif
b$=b$+t$
until v<=0
for l=len(b$) to 1 step -1
res$=res$+mid$(b$,l)
next i
endfunction res$
sync on : sync 30 : sync
decval=13
base=2
binary$=value2baser(decval,2)
octal$=value2baser(decval,base+6)
hexdec$=value2baser(decval,base+14)
`binary$=value2base(decval,2)
`octal$=value2base(decval,base+6)
`hexdec$=value2base(decval,base+14)
print str$(decval);" -> ";binary$;" (binary)"
print str$(decval);" -> ";octal$;" (octal)"
print str$(decval);" -> ";hexdec$;" (hexadecimal)"
sync
wait key
end
function value2base(v,base)
local b$, t$, rmd, v2
repeat
rmd = v mod base
v2 = v / base
`print v; ", ";v2; ", ";rmd;", base= ";base // debug only
v = v2
if base = 16
if rmd >=10 and rmd<=15
if rmd=10 then t$="A"
if rmd=11 then t$="B"
if rmd=12 then t$="C"
if rmd=13 then t$="D"
if rmd=14 then t$="E"
if rmd=15 then t$="F"
else
t$=str$(rmd)
endif
else
t$=str$(rmd)
endif
b$=b$+t$
until v<=0
for l=len(b$) to 1 step -1
res$=res$+mid$(b$,l)
next i
endfunction res$
function value2baser(v,base)
a$=value2basermain$(v,base,"")
endfunction a$
function value2basermain$(v,base,res$)
if v > 0
rmd = v mod base
v2 = v / base
`print v; ", ";v2; ", ";rmd
v = v2
if base = 16
if rmd >=10 and rmd<=15
if rmd=10 then t$="A"
if rmd=11 then t$="B"
if rmd=12 then t$="C"
if rmd=13 then t$="D"
if rmd=14 then t$="E"
if rmd=15 then t$="F"
else
t$=str$(rmd)
endif
else
t$=str$(rmd)
endif
res$=res$+t$
`print t$;", ";res$
res$=value2basermain$(v,base,res$)
endif
for l=len(res$) to 1 step -1
result$=result$+mid$(res$,l)
next i
endfunction result$
Rem ***** Main Source File *****
sync on : sync rate 30: sync
// 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 //
`bin2dec = (2^0)+(2^1)+(2^2)+(2^3)+(2^4)+(2^5)+(2^6)+(2^7)
dim binary$(15)
b$="0000000000000111"
for i=len(b$) to 1 step -1
binary$(len(b$)-i)=mid$(b$,i)
bn$=bn$+mid$(b$,i,1)
next i
decval=0
for b=0 to 15
if binary$(b)="1"
inc decval,2^b
endif
next b
print
print "decimal: ";decval
print %1111111111111001
print %1111111111111111
a= (%1111111111111001)
b= (%1111111111111111)+1
print a-b
print bin$(-7)
Sync
wait key
end