views:

74

answers:

2

I have a byte array in my C# code that I need to pass into a LuaInterface instance. I can use pack() in Lua, pass the resulting string to C# and convert it with System.Text.Encoding.UTF8.GetBytes(), but going the other way doesn't seem to work.

Is there a simple solution? I'm hoping I can avoid assigning the byte array to a global value.

Edit:
I tried a few new things this morning. I tried using LuaInterface.GetFunction(), and everything works until it hits lua_pushstring() in LuaDLL.cpp. At this point the C# string is converted to a char* via Marshal::StringToHGlobalAnsi().ToPointer(). It looks like this function expects a null terminated string, and my string's first byte is 0 so I get an empty string in my lua code.

A: 

Try encoding your byte array with System.Text.ASCIIEncoding.ASCII.GetString to get a string that can be passed to Lua.

Judge Maygarden
I tried this initially, except with UTF8 encoding. The problem is passing the data into Lua at that point. I have to call the lua function via lua.DoString("parse(_data_)"); The string formatting doesn't like the null characters.
kyork
A: 

Finally traced it down to a the call to ::lua_pushstring() in lapi.c. It called strlen() on the char* passed in. Since my first byte of data was 0, it returned 0. There is an alternate call, lua_pushlstring, that accepts the size of the string as an argument. Changing to call this function fixed the issue.

kyork