#Base Conversion

cls

dim A$(6)

A$={"A", "B", "C", "D", "E", "F"}

print "Enter 10 for Dec to Hex"

print "Enter 16 for Hex to Dec"

print "Enter 2 for Hex to Binary"

input G

if (G-2)=0 then goto Binary

if (G-10)=0 then goto Deconv

#Convert Hex to Dec

print "Enter Hex # to be converted ";:input S$

#Check length of the Hex string

N=length(S$)

y=0

#Convert Hex character one by one, for example:

#Hex(B3E)=Dec(11x16**2 + 3x16 + 14)

for I=1 to N

J$=mid(S$,I,1)

if J$=A$[0] then goto Labe3

if J$>A$[0] then goto Labe3

#Process Hex characters 0-9

s=asc(J$)-48

goto Labe4

Labe3:

#Process Hex characters A-F

gosub Loop2

Labe4:

#Add all the decimal numbers

x=G

p=N-I

gosub power

y=y+s*expx

next I

#Promote reminder>0.4 to 1

f=y-int(y)

if f>0.4 then y=int(y)+1

print "The Dec # = ";:print y

goto Done

 

Deconv:

#Convert Dec to Hex

H=16

print "Input Dec # to be converted ";:input x

x0=x

#Initialization, special processing for x=0

if x=0 then goto Labe6

S$=chr(32)

#Check length of input number, and process each one

xint=int(x)

x$=string(xint)

L=length(x$)

logint=0

if L=1 then goto Cont1

logint=(L-1)*2.3025851

xfrc=x

for k=1 to (L-1)

xfrc=xfrc/10

next k

x=xfrc

Cont1:

#Find number of output Hex characters

gosub log

log1=logx+logint

log2=2.772588

log3=log1/log2

N=int(log3)+1

x=x0

for I=1 to N

z1=(x/H)+0.00000001

z2=int(z1)

#Get reminder of x/16**I to find the Hex Character

#For example: Dec(2878)= Hex{int[16xreminder(2878/16**3)] +

# int[16xreminder(2878/16**2)] + int[16xreminder(2878/16)]} = B3E

z3=z1-z2

z=z3*H

z=int(z)

x=int(x/H)

if z>9 then goto Labe1

W$=string(z)

goto Labe8

Labe1:

#To convert Dec number to Hex character

gosub Loop1

Labe8:

S$=W$+S$

next I

goto Labe7

Labe6:

S$=chr(48)

print "The Hex # = ";:print S$

goto Done

Labe7:

print "The Hex # = ";:print S$

goto Done

 

Binary:

dim B$(16)

B$={"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}

U$=chr(32)

print "Enter Hex # to be converted ";:input S$

#Get length of input Hex characters

N=length(S$)

y=0

#Search for corresponding Binary string to each Hex character

for I=1 to N

J$=mid(S$,I,1)

#Look up Binary index for Hex 0-9

num=asc(J$)-48

for k=0 to 9

if (num-k)=0 then goto ContA

next k

#Look up Binary index for Hex A-F

num=asc(J$)-65

for k=0 to 5

if (num-k)=0 then goto ContB

next k

ContB:

k=k+10

ContA:

#Attach Binary string together with "-" in between

if I=1 then goto ContC

U$=U$+"-"+B$[k]

goto ContD

ContC:

U$=U$+B$[k]

ContD:

next I

print "The Binary # = ";:print U$

goto Done

 

log:

#For x>0

#log(x)=2[(x-1)/(x+1) + (x-1)**3/3(x+1)**3 + ...]

logx=0

n=1

x1=x-1

x1n=x1

x2=x+1

x2n=x2

x3=x1n/x2n

delta=0.00000001

MainLoop:

logx=logx+x3

if abs(x3)<delta then goto Ende

n=n+2

x1n=x1n*x1*x1

x2n=x2n*x2*x2

x3=x1n/(n*x2n)

refresh

goto MainLoop

Ende:

logx=2*logx

return

 

power:

#y=x**p=exp(p*logx)

gosub log

x=(p*logx)

gosub expe

return

 

expe:

#expx=1 + x/1! + x**2/2! + x**3/3! + ...

expx=1

xp=1

n=0

nf=1

delta=0.00000001

SubLoop2:

exp0=expx

xp=xp*x

n=n+1

nf=nf*n

expx=expx+xp/nf

if abs(expx-exp0)<delta then goto End2

refresh

goto SubLoop2

End2:

Return

 

Loop1:

#Convert Dec number to Hex character

for k=0 to 5

W$=A$[k]

if z=10+k then goto labe2

next k

labe2:

return

 

Loop2:

#Convert Hex character to Dec number

for k=0 to 5

s=10+k

if J$=A$[k] then goto Labe5

next k

Labe5:

Return

 

Done: