dmitry1204 dmitry1204 - 2 months ago 21
C# Question

Cyrillic string value in lua script with c#

I try to add lua-scripts in C# project with NLua library (nlua.org).
My problem is incorrect representation of Cyrillic symbols in string values.
My c# code is:

Lua lua = new Lua();
lua.DoFile("script.lua");
Console.WriteLine(lua["var"]);


The script file code is:

var = 'кириллица'


Changing of script file encoding does not help me. Also I tried to search the correct script file encoding with this code:

foreach (EncodingInfo ei in Encoding.GetEncodings()) {
Encoding e = ei.GetEncoding ();
string s1 = "cyrillic кириллица";
System.IO.File.Delete ("script.lua");
System.IO.File.AppendAllText ("script.lua", "var = '" + s1 + "'", e);
string s2;
try {
Lua lua = new Lua ();
lua.DoFile ("script.lua");
s2 = lua ["var"] as string;
} catch {
s2 = "error in lua";
}
Console.WriteLine ("[{0}]\t({1})", s2, e.HeaderName);
}


Here is console output:

[error in lua] (IBM037)
[cyrillic ?????????] (IBM437)
[error in lua] (IBM500)
[cyrillic ?????????] (asmo-708)
[cyrillic ?????????] (ibm850)
[cyrillic ?????????] (ibm852)
[cyrillic Æ·á·Ðз¤*] (ibm855)
[cyrillic ?????????] (ibm857)
[cyrillic ?????????] (IBM00858)
[cyrillic ?????????] (ibm860)
[cyrillic ?????????] (ibm861)
[cyrillic ?????????] (ibm861)
[cyrillic ?????????] (IBM863)
[cyrillic ?????????] (ibm864)
[cyrillic ?????????] (IBM865)
[cyrillic ª¨à¨««¨æ*] (ibm866)
[cyrillic ?????????] (ibm869)
[error in lua] (ibm870)
[cyrillic ?????????] (windows-874)
[error in lua] (ibm875)
[cyrillic
{
y

y
|
|
y

p] (iso-2022-jp)
[cyrillic §Ü§Ú§â§Ú§Ý§Ý§Ú§è§Ñ] (gb2312)
[cyrillic ¬Ü¬Ú¬â¬Ú¬Ý¬Ý¬Ú¬è¬Ñ] (ks_c_5601-1987)
[cyrillic ?????????] (big5)
[error in lua] (ibm1026)
[error in lua] (ibm1047)
[error in lua] (IBM01140)
[error in lua] (IBM01141)
[error in lua] (IBM01142)
[error in lua] (IBM01143)
[error in lua] (ibm1144)
[error in lua] (ibm1145)
[error in lua] (ibm1146)
[error in lua] (ibm1147)
[error in lua] (ibm1148)
[error in lua] (ibm1149)
[error in lua] (utf-16)
[error in lua] (utf-16BE)
[cyrillic ?????????] (windows-1250)
[cyrillic êèðèëëèöà] (windows-1251)
[cyrillic ?????????] (Windows-1252)
[cyrillic ?????????] (windows-1253)
[cyrillic ?????????] (windows-1254)
[cyrillic ?????????] (windows-1255)
[cyrillic ?????????] (windows-1256)
[cyrillic ?????????] (windows-1257)
[cyrillic ?????????] (windows-1258)
[cyrillic ?????????] (macintosh)
[cyrillic ?????????] (x-mac-icelandic)
[error in lua] (utf-32)
[error in lua] (utf-32BE)
[cyrillic ?????????] (us-ascii)
[error in lua] (IBM273)
[error in lua] (IBM277)
[error in lua] (IBM278)
[error in lua] (IBM280)
[error in lua] (IBM284)
[error in lua] (IBM285)
[error in lua] (IBM290)
[error in lua] (IBM297)
[error in lua] (IBM420)
[error in lua] (IBM424)
[cyrillic ËÉÒÉÌÌÉÃÁ] (koi8-r)
[error in lua] (IBM871)
[error in lua] (IBM1025)
[cyrillic ËÉÒÉÌÌÉÃÁ] (koi8-u)
[cyrillic ?????????] (iso-8859-1)
[cyrillic ?????????] (iso-8859-2)
[cyrillic ?????????] (iso-8859-3)
[cyrillic ?????????] (iso-8859-4)
[cyrillic ÚØàØÛÛØæÐ] (iso-8859-5)
[cyrillic ?????????] (iso-8859-6)
[cyrillic ?????????] (iso-8859-7)
[cyrillic ?????????] (iso-8859-8)
[cyrillic ?????????] (iso-8859-9)
[cyrillic ?????????] (iso-8859-15)
[cyrillic ?????????] (windows-38598)
[cyrillic ?????????] (iso-2022-jp)
[cyrillic ?????????] (iso-2022-jp)
[cyrillic ?????????] (iso-2022-jp)
[cyrillic §Ü§Ú§â§Ú§Ý§Ý§Ú§è§Ñ] (euc-jp)
[cyrillic ¬Ü¬Ú¬â¬Ú¬Ý¬Ý¬Ú¬è¬Ñ] (euc-kr)
[cyrillic §Ü§Ú§â§Ú§Ý§Ý§Ú§è§Ñ] (GB18030)
[cyrillic ?????????] (x-iscii-de)
[cyrillic ?????????] (x-iscii-be)
[cyrillic ?????????] (x-iscii-ta)
[cyrillic ?????????] (x-iscii-te)
[cyrillic ?????????] (x-iscii-as)
[cyrillic ?????????] (x-iscii-or)
[cyrillic ?????????] (x-iscii-ka)
[cyrillic ?????????] (x-iscii-ma)
[cyrillic ?????????] (x-iscii-gu)
[cyrillic ?????????] (x-iscii-pa)
[error in lua] (utf-7)
[error in lua] (utf-8)


You can see that there is no correct variant at all. So I don't know how to fix that.

Answer

I found simple solution for my project. I use

lua.DoString (System.IO.File.ReadAllText ("script.lua", enc));

instead of

lua.DoFile ("script.lua");

Here enc - is my script file encoding.