Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function toBinary (text)
- local binary = {}
- for i = 1, #text do
- local num = reverseCharList[text:sub(i, i)] - 1
- local curBit = 64
- for j = 1, 7 do
- if bit.band(curBit, num) == curBit then
- binary[#binary + 1] = "1"
- else
- binary[#binary + 1] = "0"
- end
- curBit = curBit / 2
- end
- end
- return table.concat(binary)
- end
- function fromBinary (binary)
- local text = {}
- for i = 0, #binary / 7 - 1 do
- local num = 0
- local curBit = 64
- for j = 0, 6 do
- local stringPos = i * 7 + j + 1
- if binary:sub(stringPos, stringPos) == "1" then
- num = num + curBit
- end
- curBit = curBit / 2
- end
- text[i + 1] = charList[num + 1]
- end
- return table.concat(text)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement