-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.lua
79 lines (69 loc) · 2.1 KB
/
binary.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
local bit32 = require("bit32")
local binary = {}
---Converts an integer into a little endian byte array or a given size, defaults to 4 bytes if ommited
---@param int number
---@param optional length number
---@return table
function binary.int2bytes(int, length)
length = length or 4
local result = {}
for i = 1, length do
result[i] = bit32.band(bit32.rshift(int, ((i-1) * 8)), 0xFF)
end
return result
end
---Converts a little endian byte array into a binary string
---@param bytes table
---@return string
function binary.bytes2string(bytes)
return string.char(table.unpack(bytes))
end
---Converts an integer into a little endian binary string of a given size, defaults to 4 bytes if ommited
---@param int number
---@param optional length number
---@return string
function binary.int2binString(int, length)
return binary.bytes2string(binary.int2bytes(int, length))
end
---Converts a binary string into a byte array
---@param str string
---@return table
function binary.binString2bytes(str)
local result = {}
for i = 1, string.len(str) do
result[i] = string.byte(str, i)
end
return result
end
---Converts a byte array of a little endian integer into an integer
---@param bytes table
---@return number
function binary.bytes2int(bytes)
local result = 0
for i, v in ipairs(bytes) do
result = result + bit32.lshift(v, (i-1) * 8)
end
return result
end
---Converts a binary string of a little endian integer into a integer
---@param bytes table
---@return number
function binary.string2int(str)
return binary.bytes2int(binary.string2bytes(str))
end
---Converts a binary string of a little endian integer into a integer
---@param bytes table
---@return number
function binary.binString2int(str)
return binary.bytes2int(binary.string2bytes(str))
end
local function debug()
local serial = require("serialization")
print(serial.serialize(binary.int2bytes(384)))
local test = binary.int2binString(384)
print(test)
test = binary.binString2bytes(test)
print(serial.serialize(test))
print(binary.bytes2int(test))
end
return binary