Advertisement
Freack100

Binary API for CC

Apr 3rd, 2015
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | None | 0 0
  1. -------------------------------
  2. --           MISC            --
  3. -------------------------------
  4.  
  5. --local function to split a string at a given regex
  6. local function split(str,reg)
  7.     local tbl = {}
  8.     for match in string.gmatch(str,reg) do
  9.         table.insert(tbl,match)
  10.     end
  11.     return tbl
  12. end
  13.  
  14. --Fills up a string with 0000000 until it reaches a specific length
  15. function fillString(bin,length)
  16.     local output = bin
  17.     while #bin < length do
  18.         table.insert(bin,"0000000")
  19.     end
  20.     return output
  21. end
  22.  
  23. --Fills a char-binary until it fits the "normal" format
  24. function fillChar(bin)
  25.     local c = bin
  26.     if(#c ~= 7) then
  27.         while #c ~= 7 do
  28.             c = "0"..c
  29.         end
  30.     end
  31.     return c
  32. end
  33.  
  34. -------------------------------
  35. --       CONVERSION          --
  36. -------------------------------
  37.  
  38. -- Get binary from a number
  39. function numberToBinary(number)
  40.   local s = ""
  41.   repeat
  42.     local remainder = number % 2
  43.     s = remainder..s
  44.     number = (number-remainder)/2
  45.   until number==0
  46.   return s
  47. end
  48.  
  49. -- Get a number from binary input
  50. function binaryToNumber(bin)
  51.     return tonumber(bin,2)
  52. end
  53.  
  54. -- Get binary from a char
  55. function charToBinary(ch)
  56.     return numberToBinary(string.byte(string.sub(ch,1,1)))
  57. end
  58.  
  59. -- Get a char from binary
  60. function binaryToChar(bin)
  61.     return string.char(binaryToNumber(bin))
  62. end
  63.  
  64. -- Get a binary table from a string
  65. function stringToBinary(str)
  66.  local tbl = {}
  67.  for char in string.gmatch(str,".") do
  68.     tbl[#tbl+1] = charToBinary(char)
  69.  end
  70.  return tbl
  71. end
  72.  
  73. -- Get a string from a binary table
  74. function binaryToString(bin)
  75.     str = ""
  76.     for k,v in pairs(bin) do
  77.         str = str..binaryToChar(v)
  78.     end
  79.     return str
  80. end
  81.  
  82. -------------------------------
  83. --          LOGIC            --
  84. --          SIMPLE           --
  85. -------------------------------
  86.  
  87. -- 'not' a binary sequence
  88. function bnot(bin)
  89.     local bin = split(bin,".")
  90.     local output = ""
  91.     for k,v in pairs(bin) do
  92.         output = output..(v=="1" and "0" or "1")
  93.     end
  94.     return output
  95. end
  96.  
  97. -- 'or' two binary sequences
  98. function bor(first,second)
  99.     local f,s = split(first,"."),split(second,".")
  100.     local output = ""
  101.     if(#first ~= #second) then error("binaries are not of the same length") end
  102.     for i = 1, #first do
  103.         output = output..((f[i] == "1" or s[i] == "1") and "1" or "0")
  104.     end
  105.     return output
  106. end
  107.  
  108. -- 'and' two binary sequences
  109. function band(first,second)
  110.     local f,s = split(first,"."),split(second,".")
  111.     local output = ""
  112.     if(#first ~= #second) then error("binaries are not of the same length") end
  113.     for i = 1, #first do
  114.         output = output..((f[i] == "1" and s[i] == "1") and "1" or "0")
  115.     end
  116.     return output
  117. end
  118.  
  119. -- 'Äquivalenz'
  120. function bxand(first,second)
  121.     local f,s = split(first,"."),split(second,".")
  122.     local output = ""
  123.     if(#first ~= #second) then error("binaries are not of the same length") end
  124.     for i = 1, #first do
  125.         output = output..((f[i] == s[i]) and "1" or "0")
  126.     end
  127.     return output
  128. end
  129.  
  130. -- 'Antivalenz'
  131. function bxor(first,second)
  132.     local f,s = split(first,"."),split(second,".")
  133.     local output = ""
  134.     if(#first ~= #second) then error("binaries are not of the same length") end
  135.     for i = 1, #first do
  136.         output = output..((f[i] ~= s[i]) and "1" or "0")
  137.     end
  138.     return output
  139. end
  140.  
  141. -------------------------------
  142. --          LOGIC            --
  143. --          TABLE            --
  144. -------------------------------
  145.  
  146. function btnot(bin)
  147.     local output = {}
  148.     for k,v in pairs(bin) do
  149.         table.insert(output,bnot(v))
  150.     end
  151.     return output
  152. end
  153.  
  154. -- 'or' two binary sequences
  155. function btor(first,second)
  156.     local output = {}
  157.     for k,v in pairs(first) do
  158.         table.insert(output,bor(v,second[k]))
  159.     end
  160.     return output
  161. end
  162.  
  163. -- 'and' two binary sequences
  164. function btand(first,second)
  165.     local output = {}
  166.     for k,v in pairs(first) do
  167.         table.insert(output,band(v,second[k]))
  168.     end
  169.     return output
  170. end
  171.  
  172. -- 'Äquivalenz'
  173. function btxand(first,second)
  174.     local output = {}
  175.     for k,v in pairs(first) do
  176.         table.insert(outp,bxand(v,second[k]))
  177.     end
  178.     return output
  179. end
  180.  
  181. -- 'Antivalenz'
  182. function btxor(first,second)
  183.     local output = {}
  184.     for k,v in pairs(first) do
  185.         table.insert(output,bxor(v,second[k]))
  186.     end
  187.     return output
  188. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement