Decimal to Binary converter

I hope this helps people. It’s for classes, a project, or even work. Whatever you need it for, it’s here and open sourced.
Feel free to copy code bits.

-- Byte Converter

-- Use this function to perform your initial setup
function setup()
    fontSize(50)
    displayMode(FULLSCREEN)
    Menu=false
    Number = 6 -- CHANGE THIS TO YOUR NUMBER
    Byte = DecimalToByte(Number)--DecimalToByte(Number)
    Key = ""
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 0, 0, 255)

    -- This sets the line thickness
    strokeWidth(5)
    w,h = textSize("Byte: "..Byte)
    text("Byte: "..Byte, WIDTH/2, h+10)
    text("Tap to change Decimal number.", WIDTH/2, HEIGHT/2)
    text("Press RETURN when done.", WIDTH/2, HEIGHT/4)
    if Menu == true then Key=keyboardBuffer() end
    text(keyboardBuffer(), WIDTH/2, HEIGHT-100)
    fill(255, 255, 255, 255)
    -- Do your drawing here
    
end


function DecimalToByte(num,bits)
    if pcall(function() local N = num+ num end) then else return "Dont use letters" end
    -- returns a table of bits, most significant first.
    bits = bits or select(2,math.frexp(num))
    local t={} -- will contain the bits        
    for b=bits,1,-1 do
        t[b]=tonumber(string.sub(math.fmod(num,2), 1,1))
        
        num=(num-t[b])/2
      end
     return table.concat(t)
end


function touched()
    if Menu==false then 
    Menu=true 
    showKeyboard() 
    end
end

function keyboard(key)
    if key == "\
" then
        Menu=false
        hideKeyboard()
        Byte = DecimalToByte(tonumber(Key))
    end
end 

@LuaLlama Nice program, interesting use of code. You should modify the code so you can see all the text while the keyboard shows.

Thanks Seth, can’t wait to use for GTT

@dave1707 thanks, I sure will do that, but right now I’m working on a Binary to decimal converter.

@LuaLlama Here’s a converter program I have. It converts a decimal number (base 10) to any base from 2 to 25.

supportedOrientations(PORTRAIT_ANY)

function setup()
    showKeyboard()
    textMode(CORNER)
    str="0123456789abcdefghijklmno"
    tab={}
    for z=2,25 do
        tab[z]=btn(15,1050-z*29,600,25)   
    end
    parameter.text("decimal_number",0,convert)
end

function draw()
    background(0)
    for a,b in pairs(tab) do
        b:draw(a)
    end
end

function convert()
    for z=2,25 do
        nbr=tonumber(decimal_number)
        if nbr==nil or nbr<1 then
            tab[z].v="0"
        else
            tab[z].v=""
            while nbr>0 do
                tab[z].v=str:sub(nbr%z+1,nbr%z+1)..tab[z].v
                nbr=nbr//z
            end
        end
    end
end

btn=class()

function btn:init(x,y,w,h)
    self.x=x
    self.y=y
    self.w=w
    self.h=h
    self.v=""  
end

function btn:draw(base)
    fill(190, 222, 225, 255)
    text("B-"..base,0,self.y)
    rect(self.x+32,self.y,self.w,self.h)
    fill(234, 72, 72, 255)
    text(self.v,self.x+37,self.y)
end

@LuaLlama Here’s my above program changed to convert any base 2-25 to any other base 2-25.

supportedOrientations(PORTRAIT_ANY)

function setup()
    showKeyboard()
    textMode(CORNER)
    str="0123456789abcdefghijklmno"
    tab={}
    for z=2,25 do
        tab[z]=btn(15,1050-z*29,600,25)   
    end
    parameter.text("number",0)
    parameter.text("base",10)
    parameter.action("convert",convert)
end

function draw()
    background(0)
    for a,b in pairs(tab) do
        b:draw(a)
    end
end

function convert()
    decimal=tonumber(number,base)
    for z=2,25 do
        nbr=tonumber(decimal)
        if nbr==nil or nbr<1 then
            tab[z].v="0"
        else
            tab[z].v=""
            while nbr>0 do
                tab[z].v=str:sub(nbr%z+1,nbr%z+1)..tab[z].v
                nbr=nbr//z
            end
        end
    end
end

btn=class()

function btn:init(x,y,w,h)
    self.x=x
    self.y=y
    self.w=w
    self.h=h
    self.v=""  
end

function btn:draw(base)
    fill(190, 222, 225, 255)
    text("B-"..base,0,self.y)
    rect(self.x+32,self.y,self.w,self.h)
    fill(234, 72, 72, 255)
    text(self.v,self.x+37,self.y)
end

@dave1707 Thanks so much! This will definitely help me. Your code is so much more organized than mine.