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

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

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
permanent link

answered 06 Sep '11, 10:01

SwDevMan81's gravatar image

SwDevMan81
363612
accept rate: 50%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×431
×34
×8
×6
×3

question asked: 26 Aug '11, 06:54

question was seen: 4,100 times

last updated: 06 Sep '11, 10:37

p​o​w​e​r​e​d by O​S​Q​A