base 2 thru base 36 number conversion

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

OLGDI! (HELLO from B36 to B33) This is a great application, especially when I need to turn a number into hex for CSS.

I didn’t have anything else to do, so I thought I’d post this. It’s another base conversion program. It allows you to convert any base to any other base. Just tap on the boxes and enter the values for From Base, Number, To Base, and then Convert. There is no backspace, so if you enter something wrong just tap the box and renter the value. For bases from 2 to 36, enter values from 0-9, or a-z. For bases above 36, enter numbers separated with a :. You can’t enter a single character or value: that’s equal to or higher than its base. I don’t have all kinds of error corrections, so if the program cancels, just restart it. You can convert from bases like 21234 to bases like 32345, but I’m not sure what value they have. You can use from base 36 and enter your name and see what value your name would be in base 16, 10, or base 2.

displayMode(FULLSCREEN)

function setup()
    showKeyboard()
    rectMode(CENTER)
    b1=button(WIDTH/2-250,HEIGHT-100,"From Base",100)
    b2=button(WIDTH/2+100,HEIGHT-100,"Number",500)
    b3=button(WIDTH/2-250,HEIGHT-250,"To Base",100)
    b4=button(WIDTH/2+100,HEIGHT-250,"Convert",500)
end

function draw()
    background(31, 75, 73, 112)
    b1:draw() 
    b2:draw()  
    b3:draw() 
    b4:draw() 
    text("Number      base 2-36, use NNN...     base 37->??, use N:N:N:...",WIDTH/2,HEIGHT/2+50)
end

function touched(t)
    showKeyboard()
    if t.state==BEGAN then
        b1.sel=false
        b2.sel=false
        b3.sel=false
        b4.sel=false
        b4.value=""
        b1:touched(t)
        b2:touched(t)
        b3:touched(t)
        b4:touched(t)
    end
end

function keyboard(k)
    b1:keyboard(k)
    b2:keyboard(k)
    b3:keyboard(k)
end

function convert()
    tab={}
    str2="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    if tonumber(b1.value)==nil or tonumber(b1.value)==1 then
        b4.value="Error: From Base invalid"
        return
    end
    if tonumber(b3.value)==nil or tonumber(b3.value)==1 then
        b4.value="Error: To Base invalid"
        return
    end
    if tonumber(b1.value)>36 then
        v=""
        for z=1,#b2.value do
            c=string.sub(b2.value,z,z)
            if c==":" then
                table.insert(tab,tonumber(v))
                v=""
            else
                v=v..c
            end
        end
        if v~="" then
            table.insert(tab,tonumber(v))
        end
        c=0
        for z=1,#tab do
            if tab[z]>=tonumber(b1.value) then
                b4.value="Error: "..tab[z].." is greater than From Base"
                return
            end
            c=c+tab[z]*(b1.value^(#tab-z))
            if c>99999999999999 then
                b4.value="Error: Number Overflow"
                return
            end

        end
    else
        c=0
        for z=#b2.value,1,-1 do
            s=string.sub(b2.value,z,z)     
            v=string.find(str2,s)-1
            if v>=tonumber(b1.value) then
                b4.value="Error: "..s.." is greater than From Base"
                return 
            end
            c=c+v*b1.value^(#b2.value-z)
            if c>99999999999999 then
                b4.value="Error: Number Overflow"
                return
            end
        end
    end 
    
    str1=""   
    while c>0 do
        i,f=math.modf(c/b3.value)
        r=c%b3.value
        c=i
        if tonumber(b3.value)<37 then
            if r>0 then
                str1=string.sub(str2,r+1,r+1)..str1
            else
                str1="0"..str1
            end
        else
            if r>0 then
                str1=math.tointeger(r)..":"..str1
            else
                str1="0:"..str1
            end
        end
    end
    b4.value=str1
end

button=class()

function button:init(x,y,name,width)
    self.x=x
    self.y=y   
    self.w=width
    self.h=50 
    self.name=name
    self.value=""
    self.sel=false
end

function button:draw()
    fill(255)
    text(self.name,self.x,self.y+50)
    fill(50)
    if self.sel then
        fill(150)
    end
    stroke(255)
    strokeWidth(2)
    rect(self.x,self.y,self.w,self.h)
    fill(255)
    text(self.value,self.x,self.y)
end

function button:touched(t)
    if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
            t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
        self.sel=true
        self.value=""
        if self.name=="Convert" then
            convert()
        end
    end
end

function button:keyboard(k)
    if self.sel then
        self.value=self.value..string.upper(k)
    end
end