-
-
Save jiaoyk/cc8f27243fdbb47ffe0beeed712d9f2b to your computer and use it in GitHub Desktop.
lua tonumber can't handle big integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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