This is a static archive of our old Q&A Site. Please post any new questions and answers at ask.wireshark.org.

Convert table to Byte array using CLRPackage

0

I'm trying to convert a Lua Table to a C# Byte array. I was able to get a conversion to a Double array to work as follows:

> require 'CLRPackage'
> import "System"
> tbl = {11,22,33,44}
> dbl_arr = Double[4]
> dbl_arr:GetValue(0)
> dbl_arr:GetValue(1)
> for i=0,3 do Console.WriteLine(dbl_arr:GetValue(i)) end
0
0
0
0
> for i,v in ipairs(tbl) do dbl_arr:SetValue(v,i-1) end
> for i=0,3 do Console.WriteLine(dbl_arr:GetValue(i)) end
11
22
33
44
>

However if I change the dbl_arr to a Byte array (dbl_arr = Byte[4]), then I get the following error: (error object is not a string)

I was able to get a bit more information from the error by doing this:

suc,err = pcall(function() byte_arr:SetValue(12,0) end)

Now suc is false and err returns the following message:

SetValue failed
System.ArgumentException: Cannot widen from source type to target type either
   because the source type is a not a primitive type or the conversion cannot
   be accomplished.
at System.Array.InternalSetValue(Void* target, Object value)
at System.Array.SetValue(Object value, Int32 index)

I tried doing Convert.ToByte(12) and passing that in, but it still fails. I have a feeling its storing the intermediate results as a Lua 'number', which then automatically converts it to a C# Double. This is the only explanation I can come up with based on that error.

I've tried a bunch of different things with no luck. Any help would be appreciated.

I've also asked this question on Stackoverflow to try and get some answers. I thought I would try here since it seems like Lua would be used more by people involved with Wireshark.

asked 26 Aug '11, 06:54

SwDevMan81's gravatar image

SwDevMan81
363612
accept rate: 50%

edited 06 Sep '11, 10:37

Off topic? You might get better results from stackoverflow.com.

(26 Aug '11, 14:31) helloworld

I did try SO first, but didnt get any response. I was hoping someone here might have run into this while using Wireshark or might have some ideas. I've linked the SO question in my original question.

(30 Aug '11, 11:05) SwDevMan81

One Answer:

1

I found a workaround for this issue. I'll post it here, although I'm still curious why the above doesn't work.

Here is the workaround. I basically create a MemoryStream and use the WriteByte function to force the value to a Byte (since there is no overload of the function, it only accepts a byte). Then I call ToArray to get the byte[] from the MemoryStream:

> require 'CLRPackage'
> import "System"
> tbl = {11,22,33,44}
> mem_stream = MemoryStream()
> for i,v in ipairs(tbl) do mem_stream:WriteByte(v) end
> byte_arr = mem_stream:ToArray()
> for i=0,byte_arr.Length-1 do Console.WriteLine(string.format("%d", byte_arr:GetValue(i))) end
11
22
33
44

answered 06 Sep '11, 10:01

SwDevMan81's gravatar image

SwDevMan81
363612
accept rate: 50%