-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.lua
56 lines (46 loc) · 1.04 KB
/
hex.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
---------------------------------------------------------------------------------
-- hex --
---------------------------------------------------------------------------------
local string = require "string"
function string.tohex(str)
return (str:gsub(
".",
function(c)
return string.format("%02X", string.byte(c))
end
))
end
function string.hexlify(str)
return (str:gsub(
"..",
function(cc)
return string.format("\\x%s", cc)
end
))
end
function string.fromhex(str)
return (str:gsub(
"..",
function(cc)
return string.char(tonumber(cc, 16))
end
))
end
function string.startswith(s, n)
return s:sub(1, #n) == n
end
function string.endswith(self, str)
-- return self:sub(-#str) == str
return self:sub(-(#str)) == str
end
--[[
local p = "AAAA"
local hex_p = string.tohex(p)
print(p:tohex())
print(p:tohex():hexlify())
print(hex_p:fromhex())
> lua test2.lua
> 41414141
> \x41\x41\x41\x41
> AAAA
--]]