Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Functions by Zsolt Toth
- --[[
- Copyright (c) 2016, Zsolt TΓ³th
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- ]]
- function shifter(bits,shift)
- local length = tablelength(bits)
- local newtable = { }
- for key,value in ipairs(bits) do
- newtable[(key+shift) % length] = value
- end
- return newtable
- end
- function tablelength(T)
- local count = 0
- for _ in pairs(T) do count = count + 1 end
- return count
- end
- function reverseTable(t)
- local reversedTable = {}
- local itemCount = #t
- for k, v in ipairs(t) do
- reversedTable[itemCount + 1 - k] = v
- end
- return reversedTable
- end
- function num2bin(num,byte_mode,existing_table)
- local tmp = num
- if byte_mode == nil or byte_mode == 0 or byte_mode == false then
- l = math.ceil(math.log(num)/math.log(2))
- elseif byte_mode == 256 or byte_mode == true then
- l = 8
- elseif byte_mode == 16 then
- l = 4
- end
- if existing_table == nil then
- local bits = { }
- for i = 1,l do
- if tmp >= math.pow(2,l-i) then
- bits[i] = true
- tmp = tmp - math.pow(2,l-i)
- else
- bits[i] = false
- end
- end
- return bits
- else
- local start = #existing_table
- for i = 1,l do
- if tmp >= math.pow(2,l-i) then
- existing_table[start+i] = true
- tmp = tmp - math.pow(2,l-i)
- else
- existing_table[start+i] = false
- end
- end
- return existing_table
- end
- end
- function bin2num(bits,pre_length) -- Use pre_length ONLY if you know the length in advance!
- local tmp = 0
- local length = 0
- if pre_length == nil then
- length = #bits
- else
- length = pre_length
- end
- for i=1,length do
- if bits[i] == true then
- tmp = tmp + math.pow(2,length-i)
- end
- end
- return tmp
- end
- function bin2hex(bits)
- local hex = { }
- local hexmax = math.ceil(#bits / 4)
- for i=1,hexmax do
- local tmp = { }
- for j=1,4 do
- tmp[j] = bits[((i-1)*4)+j]
- if tmp[j] == nil then tmp[j] = 0 end
- end
- hex[i]=bin2num(tmp)
- tmp = nil
- end
- return hex
- end
- function hex2bit(hex,existing_table)
- if existing_table == nil then
- local table = { }
- for key,value in ipairs(hex) do
- num2bin(value,false,table)
- end
- return table
- else
- for key,value in ipairs(hex) do
- num2bin(value,false,existing_table)
- end
- return existing_table
- end
- end
- function bin2byte(bits)
- local bytes = { }
- local bytemax = math.ceil(#bits / 8)
- for i=1,bytemax do
- local tmp = { }
- for j=1,8 do
- tmp[j] = bits[((i-1)*8)+j]
- if tmp[j] == nil then tmp[j] = 0 end
- end
- bytes[i]=bin2num(tmp)
- tmp = nil
- end
- return bytes
- end
- function byte2bit(bytes,existing_table)
- if existing_table == nil then
- local table = { }
- for key,value in ipairs(bytes) do
- num2bin(value,true,table)
- end
- return table
- else
- for key,value in ipairs(bytes) do
- num2bin(value,true,existing_table)
- end
- return existing_table
- end
- end
- function splitHex(byte)
- local hex = string.format("%02x", byte)
- return tonumber("0x" .. hex:sub(1,1)), tonumber("0x" .. hex:sub(2,2))
- end
- function joinHex(hexa,hexb)
- return tonumber("0x" .. string.format("%x",hexa) .. string.format("%x",hexb) )
- end
- function string2bytes(str)
- local bytes = { }
- for i = 1, #str do
- bytes[i] = str:byte(i)
- end
- return bytes
- end
- function bytes2string(bytes)
- local tmp = ""
- for key,value in ipairs(bytes) do
- tmp = tmp .. string.char(value)
- end
- end
- -- FormatNum by SamLie
- -- from sam_lie
- -- Compatible with Lua 5.0 and 5.1.
- -- Disclaimer : use at own risk especially for hedge fund reports :-)
- ---============================================================
- -- add comma to separate thousands
- --
- function comma_value(amount)
- local formatted = amount
- while true do
- formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
- if (k==0) then
- break
- end
- end
- return formatted
- end
- ---============================================================
- -- rounds a number to the nearest decimal places
- --
- function round(val, decimal)
- if (decimal) then
- return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
- else
- return math.floor(val+0.5)
- end
- end
- --===================================================================
- -- given a numeric value formats output with comma to separate thousands
- -- and rounded to given decimal places
- --
- --
- function format_num(amount, decimal, prefix, neg_prefix)
- local str_amount, formatted, famount, remain
- decimal = decimal or 2 -- default 2 decimal places
- neg_prefix = neg_prefix or "-" -- default negative sign
- famount = math.abs(round(amount,decimal))
- famount = math.floor(famount)
- remain = round(math.abs(amount) - famount, decimal)
- -- comma to separate the thousands
- formatted = comma_value(famount)
- -- attach the decimal portion
- if (decimal > 0) then
- remain = string.sub(tostring(remain),3)
- formatted = formatted .. "." .. remain ..
- string.rep("0", decimal - string.len(remain))
- end
- -- attach prefix string e.g '$'
- formatted = (prefix or "") .. formatted
- -- if value is negative then format accordingly
- if (amount<0) then
- if (neg_prefix=="()") then
- formatted = "("..formatted ..")"
- else
- formatted = neg_prefix .. formatted
- end
- end
- return formatted
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement