Discussion:
Lua converting a UINT64 to hex
Zadik, Maayan
2012-11-19 16:07:49 UTC
Permalink
Hi all,
I'm trying to create a validation script on my protocol dissector.
My dissector is written in C and the validation script is obviously in Lua.

I have this field that is 64bits long. I'm trying to read it in my Lua script
local data_f = Field.new("my_protocol.data")

I'm trying ti convert it to a hex string
local data = data_f()
...
Local s = string.format("%X", data.value)

and get the following error:
bad argument #2 to 'format' (number expected, got userdata)

is there no way to retrieve the value of a UINT64 field in Lua?
Maybe breaking it to 2 UINT32 or something?

Thanks so much,
Maayan
___________________________________________________________________________
Sent via: Wireshark-dev mailing list <wireshark-dev-IZ8446WsY0/***@public.gmane.org>
Archives: http://www.wireshark.org/lists/wireshark-dev
Unsubscribe: https://wireshark.org/mailman/options/wireshark-dev
mailto:wireshark-dev-request-IZ8446WsY0/***@public.gmane.org?subject=unsubscribe
Tony Trinh
2012-11-20 01:42:24 UTC
Permalink
Post by Zadik, Maayan
Hi all,
I'm trying to create a validation script on my protocol dissector.
My dissector is written in C and the validation script is obviously in Lua.
I have this field that is 64bits long. I'm trying to read it in my Lua script
local data_f = Field.new("my_protocol.data")
I'm trying ti convert it to a hex string
local data = data_f()
...
Local s = string.format("%X", data.value)
bad argument #2 to 'format' (number expected, got userdata)
is there no way to retrieve the value of a UINT64 field in Lua?
Maybe breaking it to 2 UINT32 or something?
In your code, the variable "data" is actually a list of instances of the
field in the packet (each represented by a FieldInfo object). To get the
values, you'll need to iterate the list:

for _,fieldinfo in pairs( data ) do
print( fieldinfo.label, fieldinfo.value )
end
Tony Trinh
2012-11-20 01:46:36 UTC
Permalink
Post by Tony Trinh
In your code, the variable "data" is actually a list of instances of the
field in the packet (each represented by a FieldInfo object). To get the
for _,fieldinfo in pairs( data ) do
print( fieldinfo.label, fieldinfo.value )
end
Woops, forgot to wrap data. Try this:

for _,fieldinfo in pairs({ data }) do
print( fieldinfo.label, fieldinfo.value )
end
Tony Trinh
2012-11-20 02:26:38 UTC
Permalink
Post by Tony Trinh
Post by Tony Trinh
In your code, the variable "data" is actually a list of instances of the
field in the packet (each represented by a FieldInfo object). To get the
for _,fieldinfo in pairs( data ) do
print( fieldinfo.label, fieldinfo.value )
end
for _,fieldinfo in pairs({ data }) do
print( fieldinfo.label, fieldinfo.value )
end
And to get the value in hex, you have to convert the UInt64 object to a
string, and pass the result to tonumber().


print(string.format("%X", tonumber( tostring(fieldinfo.value) ) ))
Stig Bjørlykke
2012-11-20 10:08:29 UTC
Permalink
Post by Zadik, Maayan
Local s = string.format("%X", data.value)
bad argument #2 to 'format' (number expected, got userdata)
Just try converting data.value with tostring first, like this:

local s = string.format("%X", tostring(data.value))
--
Stig Bjørlykke
___________________________________________________________________________
Sent via: Wireshark-dev mailing list <wireshark-dev-IZ8446WsY0/***@public.gmane.org>
Archives: http://www.wireshark.org/lists/wireshark-dev
Unsubscribe: https://wireshark.org/mailman/options/wireshark-dev
mailto:wireshark-dev-request-IZ8446WsY0/***@public.gmane.org?subject=unsubscribe
Loading...