如何利用Lua Disssector将Wireshark中的数据从数据包转换为整数?

我正在尝试将包中的数据转换为int,但却不起作用。我能够正确地将字段添加到子树中,但想使用数据作为整数以执行其他工作。

我想使用下面的变量len作为int,但是当我尝试使用“tonumber”方法时,“nil”被返回。我可以使用“tostring”将其转换为字符串,但是在使用to number方法时一无所获。

我看到了一些使用以下代码将其转换为整数的示例:

    local len = buf(0,4):uint32()

但是在我的机器上运行时会产生以下错误:

     Lua error: attempt to call method "uint32" (a nil value)

这是我拥有的一切代码,除了有注释的地方之外,一切都做得正确:

{rest of code}
-- myproto dissector function function
function (my_proto.dissector (buf, pkt, root)

    -- create subtree for myproto
    subtree = root:add(my_proto, buf(0))
    -- add protocol fields to subtree
    subtree:add(f_messageLength, buf(0,4))

    -- This line does not work as it returns a nil value
    local len = tonumber(buf(0,4))

    -- This line produces a "bad argument #1 to 'set' (string expected, got nil) error"
    -- add message len to info column
    pkt.cols.info:set((tostring(len))))
    end
end
{rest of code}

因此,我的问题是如何将用户数据类型转换为我可以使用的整数?

原文链接 https://stackoverflow.com/questions/10557702

点赞
stackoverflow用户6277151
stackoverflow用户6277151

这里的 buf 是一个 TvbRange 对象,没有 TvbRange.uint32() 方法。你需要用 TvbRange.uint() 方法。尝试使用以下更新:

function (my_proto.dissector (buf, pkt, root)
    subtree = root:add(my_proto, buf(0))
    subtree:add(f_messageLength, buf(0,4))

    local len = buf(0,4):uint()
    pkt.cols.info:set(len)
end
2012-05-12 02:13:36