ZNZNCOOP

bit.lua

Jul 7th, 2015
299
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bit = {}
  2. bit.uc = require("unicode")
  3.  
  4. function bit.getBits(number,length)
  5.    local bits = {}
  6.    local res = {}
  7.    while true do
  8.       local tmp = number%2
  9.       bits[#bits+1] = tmp
  10.       number = (number-tmp)/2  
  11.       if number == 0 then break end
  12.    end
  13.    if #bits < length then
  14.       for i=#bits,length-1 do
  15.          bits[#bits+1] = 0
  16.       end
  17.    end
  18.    len = #bits
  19.    for i=len,1,-1 do res[#res+1] = bits[i] end
  20.    return res
  21. end
  22.  
  23. function bit.getNumber(bits)
  24.    local number = 0
  25.    local count = 0
  26.    for i=#bits,1,-1 do
  27.       number = number+bits[i]*math.pow(2,count)
  28.       count = count+1
  29.    end
  30.    return number
  31. end
  32.  
  33. function bit.CharToBit(char,length)
  34.    if #char > 1 then return {} end
  35.    return bit.getBits(string.byte(char),length)
  36. end
  37.  
  38. function bit.WordToBit(word,length)
  39.    local bits = {}
  40.    for i=1,#word do
  41.       t = bit.CharToBit(string.sub(word,i,i),length)
  42.       for q=1,#t do
  43.          bits[#bits+1] = t[q]
  44.       end
  45.    end
  46.    return bits
  47. end
  48.  
  49. function bit.BitToWord(bits,length)
  50.    local tmp = {}
  51.    local word = ""
  52.    for i=1,#bits,length do
  53.       for q=i,i+length-1 do
  54.          tmp[#tmp+1] = bits[q]
  55.       end
  56.       word = word..bit.uc.char(bit.getNumber(tmp))
  57.       tmp = {}
  58.    end
  59.    return word
  60. end
  61.  
  62. return bit
RAW Paste Data