简体   繁体   English

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

[英]How to convert a string (hex decimal string) to uint64 in Lua?

I need to convert a sip callId (eg. 1097074724_100640573@8.8,8.8) string into requestId and I am using sha1 digest to get a hash. 我需要将一个sip callId(例如1097074724_100640573 @ 8.8,8.8)字符串转换为requestId,并且我正在使用sha1摘要来获取哈希。 I need to convert this hex-decimal into uint64_t due to internal compatibility: 由于内部兼容性,我需要将此十六进制小数转换为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

I am not sure about uint64 support in Lua. 我不确定Lua中的uint64支持。 Also, how to do the conversion? 另外,如何进行转换?

You can receive two 32-bit integer numbers from Lua as two "double" values and convert them to one "uint64" on C side. 您可以从Lua接收两个32位整数作为两个“双精度”值,并将它们转换为C端的一个“ uint64”。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM