Binary To Decimal Converter

Was wondering if there was anything to improve on this, also feel free to use it if you need it in any projects lol

local values = {
    [1] = 128,
    [2] = 64,
    [3] = 32,
    [4] = 16,
    [5] = 8,
    [6] = 4,
    [7] = 2,
    [8] = 1,
}

local Convert; Convert = function(expression)
    expression = tostring(expression)
    local overall = 0
    if expression:len() ~= 8 then
        return
    else
        for i = 1,expression:len() do
            if expression:sub(i,i) == "1" then
                overall = overall + values[i]
            end
        end
    end
    print(overall)
end

Convert(11000000)
print(0b11000000)

or

tonumber("11000000", 2)
1 Like

Yeah that is the simplest way

but i just made this for fun, shows the formula to binary aswell, sort of

local values = {
    [1] = 128,
    [2] = 64,
    [3] = 32,
    [4] = 16,
    [5] = 8,
    [6] = 4,
    [7] = 2,
    [8] = 1,
}

local Convert; Convert = function(expression)
    expression = tostring(expression)
    local overall = 0
    if #expression ~= 8 then --# is a bit more swag
        return
    else
        for i = 1,8 do --we allready know the expression is 8 long
            if expression:sub(i,i) == "1" then
                overall = overall + values[i]
            end
        end
    end
    print(overall)
end

Convert(11000000)