Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.87 KB | None | 0 0
  1. function toBinary (text)
  2.   local binary = {}
  3.  
  4.   for i = 1, #text do
  5.         local num = reverseCharList[text:sub(i, i)] - 1
  6.         local curBit = 64
  7.  
  8.         for j = 1, 7 do
  9.           if bit.band(curBit, num) == curBit then
  10.                 binary[#binary + 1] = "1"
  11.           else
  12.                 binary[#binary + 1] = "0"
  13.           end
  14.  
  15.           curBit = curBit / 2
  16.         end
  17.   end
  18.  
  19.   return table.concat(binary)
  20. end
  21.  
  22. function fromBinary (binary)
  23.   local text = {}
  24.  
  25.   for i = 0, #binary / 7 - 1 do
  26.         local num = 0
  27.         local curBit = 64
  28.  
  29.         for j = 0, 6 do
  30.           local stringPos = i * 7 + j + 1
  31.  
  32.           if binary:sub(stringPos, stringPos) == "1" then
  33.                 num = num + curBit
  34.           end
  35.  
  36.           curBit = curBit / 2
  37.         end
  38.  
  39.         text[i + 1] = charList[num + 1]
  40.   end
  41.  
  42.   return table.concat(text)
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement