LuaInterface: How to dump a LuaFunction for serialization - c#

I try to save/serialize a Lua state. I know, that I had to save/serialize all globals, which I find in _G, but didn't know how to save/serialize the LuaFunctions I find. I tried to use string.dump, but that delivers only a string with the Lua signature (5 bytes with LuaQ).
The function string.dump works, because if I call
lua.DoString("print(string.len(string.dump(CountItems)))");
I get 409 instead of 5 as the length of the string. So the dump works correct. It could be a problem in LuaInterface.
Is there someone out there, how realized a serialization of a Lua state via LuaInterface?
Best regards,
Dirk
PS: I'm using KopiLua 5.1.4 with LuaInterface 2.0.4 on VS 2010 C# Express.

Solved.
The problem was, that the binary chunk, which string.dumpreturns, was handled as a string. So it was cut at the first null byte in the string, which was after 5 bytes. Changed the call of DoString, so that it handles byte arrays correct.

Related

C# vs Delphi- both codes are giving different value for ‰

Can anyone tell me that why am I getting different values for ‰ symbol?
My Delphi7 code is-
k := ord('‰'); //k is of type longint; receiving k as 137
My C# code is-
k = (long)('‰');//k is of type long; receiving k as 8240
Please give me a solution, that how can I get the same value in C#? Because Delphi code solves my purpose.
C# encodes text as UTF-16. Delphi 7 encodes text as ANSI. Therein lies the difference. You are not comparing like with like.
You ask how to get the ANSI ordinal value for that character. Use new Encoding(codepage) to get an Encoding instance that matches your Delphi ANSI encoding. You'll need to know which code page you are using in order to do that. Then call GetBytes on that encoding instance.
For instance if your code page is 1252 then you'd write:
enc = new Encoding(1252);
byte[] bytes = enc.GetBytes("‰");
Or if you want to use the default system ANSI encoding you would use
byte[] bytes = Encoding.Default.GetBytes("‰");
One wonders why you are asking this question. Have answered many questions here, I wonder if you are performing some sort of encryption, hashing or encoding, and are using as your reference some Delphi code that uses AnsiString variables as byte arrays. In which case, whilst Encoding.GetBytes is what you asked for, it's not what you need. What you need is to stop using strings to hold binary data. Use byte arrays. In both Delphi and C#.

.NET MessagingToolkit QRCode Encode Issue

I've been using the .NET MessagingToolkit.QRCode library for some time on my websites in order to generate QR codes with custom images on top and until now I have had no problems at all with this library.
Recently, the data I need to encode has gotten larger as a result of system growth and attempting to encode this data within a QR code has been causing internal "index out of range" exceptions within the Encode method of the QRCodeEncoder class.
Through testing I have noted that the maximum number of characters this method appears to accept is 86 and anything greater than this will throw the exception. Changing the encoding type of the string passed in does not help.
I did note that the version of the library I was using was out of date, however, updating to the latest version also did not solve the problem.
Has anyone else experienced this issue with the library and found a solution? Alternatively, is there another library I could use which does not have such problems?
The exception details are below:
2012-10-01 17:39:52,098 Error: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at MessagingToolkit.QRCode.Codec.QRCodeEncoder.divideDataBy8Bits(Int32[] data, SByte[] bits, Int32 maxDataCodewords)
at MessagingToolkit.QRCode.Codec.QRCodeEncoder.calQrcode(Byte[] qrcodeData)
at MessagingToolkit.QRCode.Codec.QRCodeEncoder.Encode(String content, Encoding encoding)
at MessagingToolkit.QRCode.Codec.QRCodeEncoder.Encode(String content)
On the official page for that library there is a short description of solution for your problem: http://platform.twit88.com/news/60. I don't know if that would fix your problem though. I would find the source code of the library and debug through it to see the reason for the error.
hm... can't find the source code anywhere. So get in touch with the authors and ask them.
If that does not work out, I would change the library to something else. I have used QrCode.Net with MVC successfully and have some sample code how to integrate it to MVC, if you like.
I know this post is a little old but I ran into the same issue and discovered I had an illegal character in my string ( ” ) which the QR code did not recognize. Replaced this with a regular quote ( " ) and it worked fine.
I got same problem
QRCodeEncoder qrCE = new QRCodeEncoder();
qrCE.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCE.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
qrCE.QRCodeVersion = 1;
picQRCode.Image = qrCE.Encode(memBarcodeDataForPrint.Text, System.Text.Encoding.UTF8);
I changed following line
qrCE.QRCodeVersion = 1;
like
qrCE.QRCodeVersion = 0;
It solved.
I experienced the same problem. However I found that creating a new instance of the encoder each time I called the Encode method solved the issue.
Same problem but when change version to 0 it working.

MongoDb: After inserting a UUID with ruby, c# can't convert it to GUID

I am attempting to insert an object into mongoDB using ruby and retrieve it using c# and the NoRM driver.
All seemed to be progressing well until I wanted to use a Guid within my c# object.
I used the following code to set a UUID in ruby before inserting it into mongo (as suggested by this blog post http://blog.mikeobrien.net/2010/08/working-with-guids-in-mongodb-with-ruby.html):
BSON::Binary.new("d7b73eed91c549bfaa9ea3973aa97c7b", BSON::Binary::SUBTYPE_UUID)
When retrieving this object in c# the exception "Byte array for GUID must be exactly 16 bytes long." was thrown.
Using the administrative shell I inspected the contents of the object. The guid property had been set to
BinData(3,"ZDdiNzNlZWQ5MWM1NDliZmFhOWVhMzk3M2FhOTdjN2I=")
However if I inserted the same Guid using c# the guid property was set to
BinData(3,"7T6318WRv0mqnqOXOql8ew==")
Any ideas what I'm doing wrong?
I think that blog example is just wrong. It looks to me like you want the guid to be a hexstring, ie starting with "\xd7" (one byte) not "d7"
I tried this:
guidpack=guid.scan(/../).map {|e| e.to_i(16)}.pack('c*')
And checked the Base64 encoded size, it looks right now.
Base64.encode64 BSON::Binary.new(guidpack, BSON::Binary::SUBTYPE_UUID).to_s
=> "17c+7ZHFSb+qnqOXOql8ew==\n"
But the result doesn't exactly match what happens when you use C# above, so this might not be the right answer at all. (I'm not testing with mongo etc, just the bson gem, so can't check sorry)

Passing a C# byte array to LuaInterface

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.
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.
Try encoding your byte array with System.Text.ASCIIEncoding.ASCII.GetString to get a string that can be passed to Lua.

fmodex returning ERR_FILE_BAD playing an MP3 file under Mono/Ubuntu

I'm trying to use fmodex 4.30.03 to play an MP3 file under Mono/Ubuntu.
My call to createSound() looks as follows:
result = system.createSound(path,
(FMOD.MODE._2D | FMOD.MODE.HARDWARE | FMOD.MODE.CREATESTREAM),
ref sound);
as per the C# examples that come with the SDK.
result is being set to 19, ERR_FILE_BAD.
The same thing works fine under Windows. I have the following in app.config:
<dllmap os="linux" dll="fmodex" target="./libfmodex-4.30.03.so"/>
If this isn't present, fmodex never even gets loaded, so I know it's getting so far.
The file I'm passing in definitely exists, but if I pass an invalid path I actually get the same error message.
I'm assuming you managed to get the FMOD system initialized and everything. The C# wrapper passes strings through to FMOD for createSound as unicode which FMOD doesn't support on Linux. To fix this you will need to alter the C# wrapper, remove the FMOD_UNICODE flag being passed in and ensure the strings being fed into FMOD are normal ASCII.

Categories