@SkyTheCoder - picking up @dave1707’s suggestion, try this under 30 line version
It’s not only shorter, but it avoids using power functions in the binary conversion. Power functions are very slow, so an iterative approach works better.
function setup()
txt="Lorem Ipsum"
b=TextToBinary(txt)
print(b)
print(BinaryToText(b))
end
function TextToBinary(txt)
local bin=""
for c in txt:gmatch(".") do
local s,n="",c:byte()
while n>0 do
n,y= math.modf(n/2)
s=(y*2)..s
end
bin=bin..string.rep("0",8-#s)..s
end
return bin
end
function BinaryToText(bin)
local s=""
for i=1,#bin,8 do --process 8 chars at a time
s=s..string.char(tonumber(string.sub(bin,i,i+7),2))
end
return s
end