Here’s a program that will allow you to convert numbers in any base from 2 to 36. Tap the blue area of the base you want to convert. That box will be surrounded in red and allow you to key in the number/letters. The converted number will show in the other bases as you key the number. You have to hide the keyboard if you want to see or select the bases from 26 to 36. The numbers in ( ) next to each base is the range of numbers/letters you can enter for that base. Any errors will result in a 0 showing in the other bases. You can use the backspace key to correct an error. There are times you’ll get wrong answers instead of 0’s in the other bases if you key a value at the limit of the base you selected. I haven’t taken the time to try and fix that because I don’t think it will be a problem. If it’s a problem for you, feel free to fix it. If you want to see what your name is converted to in the other bases, select base 36 and key in your name. Spaces aren’t allowed and will result in 0’s showing.
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
function setup()
rectMode(CENTER)
str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
dtab={}
for z=2,36 do
table.insert(dtab,input(1060-z*29,z))
end
showKeyboard()
end
function draw()
background(40, 40, 50)
for d=1,#dtab do
dtab[d]:convert()
dtab[d]:draw()
end
end
function keyboard(k)
for d=1,#dtab do
dtab[d]:keyboard(k)
end
end
function touched(t)
if not isKeyboardShowing() then
showKeyboard()
end
for d=1,#dtab do
dtab[d]:touched(t)
end
end
input = class()
function input:init(y,b)
self.x=450
self.y=y
self.width=600
self.height=25
self.base=b
self.left=self.x-self.width/2
self.right=self.x+self.width/2;
self.bottom=self.y-self.height/2
self.top=self.y+self.height/2
self.str="0"
self.selected=false
self.max=str:sub(self.base,self.base)
end
function input:convert()
if self.selected then
return
end
decimal=tonumber(number,base)
nbr=decimal
if nbr==nil or nbr<1 then
self.str="0"
else
self.str=""
while nbr>0 do
self.str=str:sub(nbr%self.base+1,nbr%self.base+1)..self.str
nbr=nbr//self.base
end
end
end
function input:draw()
strokeWidth(0)
if self.selected then
stroke(255,0,0)
strokeWidth(3)
end
fill(123, 185, 215, 255)
rect(self.x,self.y,self.width,self.height)
if self.str~="" and self.str~=nil then
fill(10, 9, 9, 255)
text(self.str,self.x,self.y)
end
fill(255)
text("base "..self.base.." (0-"..self.max..")",60,self.y)
end
function input:touched(t)
if t.state==BEGAN then
if t.x>self.left and t.x<self.right and
t.y>self.bottom and t.y<self.top then
for c=1,#dtab do
dtab[c].selected=false
dtab[c].str="0"
number="0"
end
self.selected=true
self.str=""
base=self.base
end
end
end
function input:keyboard(k)
if self.selected then
if k==RETURN then
self.selected=false
elseif k==BACKSPACE then
self.str=string.sub(self.str,1,#self.str-1)
else
self.str=self.str..k
end
number=self.str
end
end