Advertisement
Metalhead33

ComputerCraft CoreFuncs

Nov 3rd, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.92 KB | None | 0 0
  1. -- Functions by Zsolt Toth
  2.  
  3. --[[
  4. Copyright (c) 2016, Zsolt TΓ³th
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. SOFTWARE.
  23.  
  24. ]]
  25.  
  26. function shifter(bits,shift)
  27.     local length = tablelength(bits)
  28.     local newtable = { }
  29.     for key,value in ipairs(bits) do
  30.         newtable[(key+shift) % length] = value
  31.     end
  32.     return newtable
  33. end
  34.  
  35. function tablelength(T)
  36.   local count = 0
  37.   for _ in pairs(T) do count = count + 1 end
  38.   return count
  39. end
  40.  
  41. function reverseTable(t)
  42.     local reversedTable = {}
  43.     local itemCount = #t
  44.     for k, v in ipairs(t) do
  45.         reversedTable[itemCount + 1 - k] = v
  46.     end
  47.     return reversedTable
  48. end
  49.  
  50. function num2bin(num,byte_mode,existing_table)
  51.     local tmp = num
  52.     if byte_mode == nil or byte_mode == 0 or byte_mode == false then
  53.         l = math.ceil(math.log(num)/math.log(2))
  54.     elseif byte_mode == 256 or byte_mode == true then
  55.         l = 8
  56.     elseif byte_mode == 16 then
  57.         l = 4
  58.     end
  59.     if existing_table == nil then
  60.         local bits = { }
  61.         for i = 1,l do
  62.             if tmp >= math.pow(2,l-i) then
  63.                 bits[i] = true
  64.                 tmp = tmp - math.pow(2,l-i)
  65.             else
  66.                 bits[i] = false
  67.             end
  68.         end
  69.         return bits
  70.     else
  71.         local start = #existing_table
  72.         for i = 1,l do
  73.             if tmp >= math.pow(2,l-i) then
  74.                 existing_table[start+i] = true
  75.                 tmp = tmp - math.pow(2,l-i)
  76.             else
  77.                 existing_table[start+i] = false
  78.             end
  79.         end
  80.         return existing_table
  81.     end
  82. end
  83.  
  84. function bin2num(bits,pre_length) -- Use pre_length ONLY if you know the length in advance!
  85.     local tmp = 0
  86.     local length = 0
  87.     if pre_length == nil then
  88.         length = #bits
  89.     else
  90.         length = pre_length
  91.     end
  92.     for i=1,length do
  93.         if bits[i] == true then
  94.             tmp = tmp + math.pow(2,length-i)
  95.         end
  96.     end
  97.     return tmp
  98. end
  99.  
  100. function bin2hex(bits)
  101.     local hex = { }
  102.     local hexmax = math.ceil(#bits / 4)
  103.     for i=1,hexmax do
  104.         local tmp = { }
  105.         for j=1,4 do
  106.             tmp[j] = bits[((i-1)*4)+j]
  107.             if tmp[j] == nil then tmp[j] = 0 end
  108.         end
  109.         hex[i]=bin2num(tmp)
  110.         tmp = nil
  111.     end
  112.     return hex
  113. end
  114.  
  115. function hex2bit(hex,existing_table)
  116.         if existing_table == nil then
  117.             local table = { }
  118.             for key,value in ipairs(hex) do
  119.                 num2bin(value,false,table)
  120.             end
  121.             return table
  122.         else
  123.             for key,value in ipairs(hex) do
  124.                 num2bin(value,false,existing_table)
  125.             end
  126.             return existing_table
  127.         end
  128. end
  129.  
  130. function bin2byte(bits)
  131.     local bytes = { }
  132.     local bytemax = math.ceil(#bits / 8)
  133.     for i=1,bytemax do
  134.         local tmp = { }
  135.         for j=1,8 do
  136.             tmp[j] = bits[((i-1)*8)+j]
  137.             if tmp[j] == nil then tmp[j] = 0 end
  138.         end
  139.         bytes[i]=bin2num(tmp)
  140.         tmp = nil
  141.     end
  142.     return bytes
  143. end
  144.  
  145. function byte2bit(bytes,existing_table)
  146.         if existing_table == nil then
  147.             local table = { }
  148.             for key,value in ipairs(bytes) do
  149.                 num2bin(value,true,table)
  150.             end
  151.             return table
  152.         else
  153.             for key,value in ipairs(bytes) do
  154.                 num2bin(value,true,existing_table)
  155.             end
  156.             return existing_table
  157.         end
  158. end
  159.  
  160. function splitHex(byte)
  161.     local hex = string.format("%02x", byte)
  162.     return tonumber("0x" .. hex:sub(1,1)), tonumber("0x" .. hex:sub(2,2))
  163. end
  164.  
  165. function joinHex(hexa,hexb)
  166.     return tonumber("0x" .. string.format("%x",hexa) .. string.format("%x",hexb)  )
  167. end
  168.  
  169. function string2bytes(str)
  170.     local bytes = { }
  171.     for i = 1, #str do
  172.         bytes[i] = str:byte(i)
  173.     end
  174.     return bytes
  175. end
  176.  
  177. function bytes2string(bytes)
  178.     local tmp = ""
  179.     for key,value in ipairs(bytes) do
  180.         tmp = tmp .. string.char(value)
  181.     end
  182. end
  183.  
  184. -- FormatNum by SamLie
  185.  
  186. -- from sam_lie
  187. -- Compatible with Lua 5.0 and 5.1.
  188. -- Disclaimer : use at own risk especially for hedge fund reports :-)
  189.  
  190. ---============================================================
  191. -- add comma to separate thousands
  192. --
  193. function comma_value(amount)
  194.   local formatted = amount
  195.   while true do  
  196.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  197.     if (k==0) then
  198.       break
  199.     end
  200.   end
  201.   return formatted
  202. end
  203.  
  204. ---============================================================
  205. -- rounds a number to the nearest decimal places
  206. --
  207. function round(val, decimal)
  208.   if (decimal) then
  209.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  210.   else
  211.     return math.floor(val+0.5)
  212.   end
  213. end
  214.  
  215. --===================================================================
  216. -- given a numeric value formats output with comma to separate thousands
  217. -- and rounded to given decimal places
  218. --
  219. --
  220. function format_num(amount, decimal, prefix, neg_prefix)
  221.   local str_amount,  formatted, famount, remain
  222.  
  223.   decimal = decimal or 2  -- default 2 decimal places
  224.   neg_prefix = neg_prefix or "-" -- default negative sign
  225.  
  226.   famount = math.abs(round(amount,decimal))
  227.   famount = math.floor(famount)
  228.  
  229.   remain = round(math.abs(amount) - famount, decimal)
  230.  
  231.         -- comma to separate the thousands
  232.   formatted = comma_value(famount)
  233.  
  234.         -- attach the decimal portion
  235.   if (decimal > 0) then
  236.     remain = string.sub(tostring(remain),3)
  237.     formatted = formatted .. "." .. remain ..
  238.                 string.rep("0", decimal - string.len(remain))
  239.   end
  240.  
  241.         -- attach prefix string e.g '$'
  242.   formatted = (prefix or "") .. formatted
  243.  
  244.         -- if value is negative then format accordingly
  245.   if (amount<0) then
  246.     if (neg_prefix=="()") then
  247.       formatted = "("..formatted ..")"
  248.     else
  249.       formatted = neg_prefix .. formatted
  250.     end
  251.   end
  252.  
  253.   return formatted
  254. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement