Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --254 is end of line
- --255 is end of stream
- line = "back"
- function dec2Bin(dec2)
- dec = tonumber(dec2)
- dec=dec*2
- local bin=""
- for i=0,7 do
- bin=bin..tostring(math.ceil(math.floor(dec/2)%2))
- dec=math.floor(dec/2)
- end
- return string.reverse(bin)
- end
- function bin2Dec(bin)
- return tonumber(bin, 2)
- end
- function toBinary(str)
- binary = ""
- for i=1, #str do
- local a = str:sub(i, i)
- if a == "\n" then
- b = dec2Bin(254)
- else
- local b = dec2Bin(string.byte(a))
- binary = binary..b
- end
- end
- return binary
- end
- function fromBinary(str)
- count = 0
- actualbit = ""
- text = ""
- print("Sending "..#str * 8.." bytes")
- for i=1, #str do
- count = count+1
- actualbit = actualbit..str:sub(i,i)
- if count==8 then
- if actualbit==dec2Bin(254) then
- text = text.."\n"
- else
- text = text..string.char(bin2Dec(actualbit))
- actualbit = ""
- count = 0
- end
- end
- end
- count = nil
- actualbit = nil
- return text
- end
- function send(filename)
- file = fs.open(filename, "r")
- local a = file.readAll()
- file.close()
- local b = toBinary(a)
- rs.setOutput(line, true)
- sleep(0)
- rs.setOutput(line, false)
- for i=1, #b do
- local c = b:sub(i, i)
- if c=="1" then
- rs.setOutput(line, true)
- sleep(0)
- rs.setOutput(line, false)
- else
- sleep(0)
- end
- end
- end
- function receive(filename)
- bin = ""
- actualbit = ""
- count = 0
- os.pullEvent("redstone")
- while true do
- sleep(0)
- count = count+1
- if rs.getInput(line) then
- actualbit = actualbit.."1"
- else
- actualbit = actualbit.."0"
- end
- if count==8 then
- if actualbit == dec2Bin(255) then break end
- bin = bin..actualbit
- actualbit = ""
- count = 0
- end
- end
- actualbit = nil
- count = nil
- text = fromBinary(bin)
- file = fs.open(filename, "w")
- file.write(text)
- file.close()
- end
- args = {...}
- if #args==2 then
- if args[1]=="-send" then
- send(args[2])
- else
- receive(args[2])
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment