Skip to content

Instantly share code, notes, and snippets.

@luxiao
Last active August 11, 2022 03:55
Show Gist options
  • Save luxiao/ca8e25d19041da5fb3119503e80f532d to your computer and use it in GitHub Desktop.
Save luxiao/ca8e25d19041da5fb3119503e80f532d to your computer and use it in GitHub Desktop.
lua tonumber can't handle big integer
function tobignum(v, f, step)
-- 自带tonumber遇到大整数溢出
-- v is a md5 hexdigest value, f is base format like binary or hex, use step to slice v
if f == nil then
f = 16
end
if step == nil then
step = 8
end
local len = string.len(v)
local term = math.ceil(len/step)
local t = 0
for i=1, term do
local start = step*(i-1) + 1
local seg = string.sub(v, start, start+step-1)
local m = math.pow(f, len - i*step)
t = t + tonumber(seg, f) * m
--ngx.log(ngx.ERR, '---start',start ,'---v', seg, '---m', m, '---t', t)
end
return t
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment