如何在Lua中将字符串(十六进制十进制字符串)转换为uint64?

时间:2018-03-21 22:44:43

标签: lua sha uint64

我需要将一个sip callId(例如,1097074724_100640573 @ 8.8,8.8)字符串转换为requestId,并且我使用sha1摘要来获取哈希值。由于内部兼容性,我需要将此十六进制小数转换为uint64_t:

--
-- Obtain request-id from callId
-- 
--  Returns hash
--
function common_get_request_id( callId )
   local command = "echo -n \"" .. callId .. "\" | openssl sha1 | sed 's/(stdin)= //g'"
   local handle = assert( io.popen( command, "r" ) )
   local output = handle:read( "*all" )
   local outputHash = string.gsub(output, "\n", "") -- strip newline
   handle:close()

   -- How to convert outputHash to uint64?  
end

我不确定Lua中的uint64支持。另外,如何进行转换?

1 个答案:

答案 0 :(得分:0)

你可以从Lua收到两个32位整数作为两个" double"将它们转换为一个" uint64"在C方面。

outputHash = '317acf63c685455cfaaf1c3255eeefd6ca3c5571'

local p = 4241942993 -- some prime below 2^32
local c1, c2 = 0, 0

for a, b in outputHash:gmatch"(%x)(%x)" do
   c1 = (c1 * 16 + tonumber(a, 16)) % p
   c2 = (c2 * 16 + tonumber(b, 16)) % p
end

return c1, c2  -- both numbers 0..(2^32-1)