forked from alexsilva/luabit-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.lua
94 lines (76 loc) · 1.95 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
--------------------
--Hex v0.4
--------------------
--Hex conversion lib for lua.
--How to use:
-- hex.to_hex(n) -- convert a number to a hex string
-- hex.to_dec(hex) -- convert a hex string(prefix with '0x' or '0X') to number
--Part of LuaBit(http://luaforge.net/projects/bit/).
--Under the MIT license.
--copyright(c) 2006~2007 hanzhao ([email protected])
--------------------
dofile('bit')
do
local to_hex = function (n)
if (type(n) ~= "number") then
error("non-number type passed in.")
end
-- checking not float
if (n - floor(n) > 0) then
error("trying to apply bitwise operation on non-integer!")
end
if (n < 0) then
-- negative
n = bit.tobits(bit.bnot(abs(n)) + 1)
n = bit.tonumb(n)
end
hex_tbl = { 'A', 'B', 'C', 'D', 'E', 'F' }
hex_str = ""
while (n ~= 0) do
last = mod(n, 16)
if (last < 10) then
hex_str = tostring(last) .. hex_str
else
hex_str = hex_tbl[last - 10 + 1] .. hex_str
end
n = floor(n / 16)
end
if (hex_str == "") then
hex_str = "0"
end
return "0x" .. hex_str
end
local to_dec = function(hex)
if (type(hex) ~= "string") then
error("non-string type passed in.")
end
head = strsub(hex, 1, 2)
if (head ~= "0x" and head ~= "0X") then
error("wrong hex format, should lead by 0x or 0X.")
end
v = tonumber(strsub(hex, 3), 16)
return v;
end
--------------------
-- hex lib interface
hex = {
to_dec = to_dec,
to_hex = to_hex,
}
end
--[[
-- test
d = 4341688
h = hex.to_hex(d)
--print(h)
--print(hex.to_dec(h))
i = 1
while i < 100000 do
h = hex.to_hex(i)
d = hex.to_dec(h)
if(d ~= i) then
error("failed " .. i .. ", " .. h)
end
i = i + 1
end
--]]