Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local functions = {}
- functions.decimalToBinary = function(n)
- local b = {}
- for i = 7, 0, -1 do
- if n/2^i >= 1 then
- b[8 - i] = 1
- n = n - 2^i
- else
- b[8 - i] = 0
- end
- end
- return table.concat(b)
- end
- functions.binaryToASCII = function(b)
- local ASCII = 0
- for i = 1, 8 do
- ASCII = ASCII + tonumber(b:sub(i, i)) * 2^(8 - i)
- end
- return ASCII
- end
- functions.textToBinary = function(s)
- local b = {}
- s = tostring(s)
- for i = 1, s:len() do
- b[i] = functions.decimalToBinary(s:sub(i, i):byte())
- end
- return table.concat(b)
- end
- functions.binaryToText = function(b)
- local s = {}
- b = tostring(b)
- for i = 1, b:len(), 8 do
- s[1 + ((i - 1)/8)] = string.char(functions.binaryToASCII(b:sub(i, i + 8)))
- end
- return table.concat(s)
- end
- return functions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement