Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local byte = {}
- function byte.split(str, a, b)
- local insert = table.insert
- if type(a) == 'table' then
- local t = {}
- local cursor = 0
- for i = 1, #a do
- insert(t, str:sub(cursor+1, cursor+a[i]))
- cursor = cursor + #(t[#t])
- end
- return b and unpack(t) or t
- end
- a, b = a or 0, b or 1
- return str:sub(a+1, a+b)
- end
- local max_int = 2^8
- function byte.encode(num, len)
- local insert, char = table.insert, string.char
- if len and num > max_int^len - 1 then
- error('Encode too big number: %d, max is %d', num, max^len)
- end
- local t={}
- while num > 0 do
- local rest = num % 255
- insert(t, char(rest))
- num = (num - rest) / 255
- end
- if len and #t < len then insert(t, ('\0'):rep(len - #t)) end
- return table.concat(t)
- end
- function byte.decode(c)
- local insert = table.insert
- local n = 0
- c = c:reverse()
- for j = 1, #c do
- n = n + c:sub(j, j):byte()*255^(#c-j)
- end
- return n
- end
- return byte
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement