This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
FuuuAInfiniteLoop(F.A.I.L)'s profile picture

Text and numbers to binary

Started by FuuuAInfiniteLoop(F.A.I.L), 31 March 2013 - 06:27 AM
FuuuAInfiniteLoop(F.A.I.L) #1
Posted 31 March 2013 - 08:27 AM
Its a way to convert all a lua program to a binary representation and then recover that program

ex:

args = {...}
file = fs.open(args[1], "r")
a = tobynary(file.readAll())

b = frombinary(a)
file = fs.open("output", "w")
file.write(B)/>
file.close()
FuuuAInfiniteLoop(F.A.I.L) #2
Posted 31 March 2013 - 08:35 AM
I founded, if somebody is interested this is the code;


function toBase(str, base)  --Encodes @str in @base
if not base then return nil end
str = tostring(str)
local res = ""
for i = 1, str:len() do
  if i == 1 then
   res = basen(str:byte(i), base)
  else
   res = res..":"..basen(str:byte(i), base)
  end
end
return res
end
function fromBase(str, base)  --Decodes @str from @base
if not base then return nil end
str = tostring(str)
local bytes = seperate(str, ":")
local res = ""
for i = 1, #bytes do
  res = res..(string.char(basen(tonumber(bytes[i], base), 10)))
end
return res
end
function toBinary(str)  --Encodes @str in binary
if not str then return nil end
str = tostring(str)
return toBase(str, 2)
end
function fromBinary(str)  --Decodes @str from binary
if not str then return nil end
str = tostring(str)
return fromBase(str, 2)
en
JokerRH #3
Posted 31 March 2013 - 02:48 PM
So this is a working program, isn't it?
If yes then you should probably move it to programs…:P/>
theoriginalbit #4
Posted 31 March 2013 - 03:18 PM
you know there is a way to read and write files in binary mode. when opening the file use 'rb' or 'wb' and it will be open in binary mode.