Skip to content

Instantly share code, notes, and snippets.

@Jordach
Last active Dec 25, 2015
Embed
What would you like to do?
If the number fits in an 8 bit byte, then prepend 0s to make full 8 bit bytes, numbers above 255 get new digits added (like normal binary)
function DecToBase(IN,BASE)
local B,K,OUT,D=BASE or 10,"0123456789ABCDEFGHIJKLMNOPQRSTUVW",""
while IN>0 do
IN,D=math.floor(IN/B),math.mod(IN,B)+1
OUT=string.sub(K,D,D)..OUT
end
if string.len(OUT) == 1 then
OUT = "0000000" .. OUT
elseif string.len(OUT) == 2 then
OUT = "000000" .. OUT
elseif string.len(OUT) == 3 then
OUT = "00000" .. OUT
elseif string.len(OUT) == 4 then
OUT = "0000" .. OUT
elseif string.len(OUT) == 5 then
OUT = "000" .. OUT
elseif string.len(OUT) == 6 then
OUT = "00" .. OUT
elseif string.len(OUT) == 7 then
OUT = "0" .. OUT
elseif string.len(OUT) == 8 then
OUT = OUT
else
OUT = OUT
end
return OUT
end
print 'What is your number?'
number = io.read()
number = tonumber(number)
print (DecToBase(number,2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment