如何在lua中将32字节的char缓冲区转换为uint64?

时间:2018-03-29 21:32:45

标签: lua char buffer uint64

我正在编写一种机制来生成从sha256派生的唯一ID。我用:

require 'sha2'

function getUint64ID( callId )
  local minimumValue = 100000000000000000
  local maximumValue = 999999999999999999

  outputHash = sha2.sha256hex(callId)
  print(outputHash .. "\n")

  local c1 = ""

  for a, b in outputHash:gmatch"(%x)(%x)" do
    hexTuple = tostring(a) .. tostring(b)
    intVal   = tonumber(hexTuple, 16)
    c1 = c1 .. string.char(intVal)
  end

  uint64ID = ( minimumValue + ( tonumber(c1) % ( maximumValue - minimumValue + 1 ) ) )

  print('uint64ID:')
  print(string.format("%18.0f",uint64ID))
end

getUint64ID("test")

我在上面的代码中遇到以下错误:

stdin:17: attempt to perform arithmetic on a nil value
stack traceback:
    stdin:17: in function 'getUint64ID'
    stdin:1: in main chunk
    [C]: ?

如何将32字节字符缓冲区(c1)转换为lua中的uint64数字?

0 个答案:

没有答案