How can I convert a decimal number to binary, and back to lua? Are there any native functions for such operations?

1 answer 1

local bnum = '1000001010101' print(tonumber(bnum, 2)) -- 4181 function tobin(num) local tmp = {} repeat tmp[#tmp+1]=num%2 num = math.floor(num/2) until num==0 return table.concat(tmp):reverse() end local bnum2 = tobin(4181) print(bnum==bnum2, bnum2) -- true 1000001010101