简体   繁体   English

通过Lua将C ++ wchar_t导入Flash

[英]Get C++ wchar_t into Flash, via Lua

I am currently working on an application in C++, that ties into Lua, that ties into Flash (in that order). 我目前正在使用C ++开发应用程序,该应用程序与Lua关联,并与Flash(按此顺序)关联。 My goal at the moment is getting wchar_t s from C++ into Flash, via Lua. 目前,我的目标是通过Lua将wchar_t从C ++导入Flash。 I would love any insights as to how I can accomplish this! 对于我如何实现这一目标,我将不胜感激!

If any other information is required, please ask and I'll do my best to provide it 如果需要其他任何信息,请询问,我会尽力提供


What I have tried 我尝试过的

It's my understanding that Lua is not a fan of Unicode, but it should still be able to receive the string of bytes from my C++ application. 据我了解,Lua不喜欢Unicode,但是它仍然应该能够从我的C ++应用程序接收字节字符串。 I imagine there must be a way to then pass those bytes over to Flash to then render out my intended Unicode. 我想必须有一种方法可以将这些字节传递给Flash,然后呈现出我想要的Unicode。 So what I've done so far: 所以到目前为止我已经做了:

C++: C ++:

//an example wchar_t*
const wchar_t *text = L"Test!";

//this function pushes a char* to my Lua code
lua.PushString((char*)text); //directly casting text to a char*... D:

Lua: 卢阿:

theString = FunctionThatGetsWCharFromCpp();
flash.ShowString(theString);

Flash: 闪:

function ShowString(theString:String)
{
    myTextField.text = theString;
}

Now the outcome here is that myTextField only shows "T". 现在,这里的结果是myTextField仅显示“ T”。 This made sense to me. 这对我来说很有意义。 The cast from wchar_t to char would end up padding out the char s with some zeros, especially since "T" doesn't really utilize both bytes of a wchar_t . wchar_tchar最终会用一些零填充char ,特别是因为“ T”并没有真正利用wchar_t两个字节。 A quick look at the documentation yields: 快速浏览一下文档产生的结果:

lua_pushstring lua_pushstring

The string cannot contain embedded zeros; 该字符串不能包含嵌入式零。 it is assumed to end at the first zero. 假定以第一个零结束。

So I ran a little test: 所以我进行了一些测试:

C++: C ++:

//prefixing with a Japanese character 
//which will use both bytes of the wchar_t
const wchar_t *text = L"たTest!";

The Flash textbox now reads: "_0T", 3 characters. Flash文本框现在显示为:“ _ 0T”,3个字符。 Makes total sense, the 2 bytes of the Japanese character + T, then termination. 完全有意义,日语字符+ T的2个字节,然后终止。

I understand what is going on, but I am still completely unsure of how to tackle this problem. 我了解发生了什么,但是我仍然不确定如何解决此问题。 And I'm really unsure of what to search for. 我真的不确定要搜索什么。 Is there a specific Lua function I can use to pass a wad of bytes over to Lua from C++ (I've read somewhere that lua_pushlstring is often used for this, but that also terminates at first zero)? 我是否可以使用特定的Lua函数将一堆字节从C ++传递到Lua(我读过某个地方经常使用lua_pushlstring ,但它也以第一个零终止)? Is there a Flash datatype that will accept these bytes, then I'll need to do some sort of conversion to get them into a readable, multibyte string? 是否有Flash数据类型可以接受这些字节,然后需要进行某种转换才能将它们转换为可读的多字节字符串? or is this just really not possible? 还是这真的不可能?

Note: 注意:
I'm not too familiar with Unicode and code pages and whatnot, so I'm not too sure if there'll also be a step where I'll need to specify the correct encoding in Flash so that I can get the correct output - but I'm happy to cross that bridge when I get there, but if anyone has any insight here too, that would be great! 我不太熟悉Unicode和代码页等,所以我不太确定是否还有步骤需要在Flash中指定正确的编码,以便获得正确的输出-但是当我到达那座桥时,我很高兴过桥,但是如果有人在这里也有任何见识,那就太好了!

I don't know if this will work, but I'd recommend trying to use UTF-8 . 我不知道这是否行得通,但是我建议尝试使用UTF-8 A string encoded in UTF-8 doesn't have any embedded zeros in it, so Lua should be able handle it, and Flash ought to also be able to handle it, depending on how exactly the languages interface. 以UTF-8编码的字符串中没有任何嵌入的零,因此Lua应该能够处理它,而Flash也应该能够处理它,具体取决于语言的接口方式。

Here's one way to convert a wide-character string to UTF-8 using setlocale(3) wcstombs(3) : 这是一种使用setlocale(3) wcstombs(3)将宽字符字符串转换为UTF-8的方法:

// Error checking omitted for expository purposes

// Call this once at program startup.  If you'd rather not change the locale,
// you can instead write your own conversion routine (but beware of UTF-16
// surrogate pairs if you do)
setlocale(LC_ALL, "en_US.UTF-8");

// Do this for each string you want to convert
const wchar_t *wideString = L"たTest!";
size_t len = wcslen(wideString);
size_t maxUtf8len = 4 * len + 1;  // Each wchar_t encodes to a max of 4 bytes
char *utf8String = new char[maxUtf8len];
wcstombs(utf8String, wideString, maxUtf8len);
...
// Do stuff with utf8string
...
delete [] utf8String;

If you're on Windows, you can instead use the WideCharToMultiByte function with the CP_UTF8 code page to do the conversion, since I don't believe that the Visual Studio C runtime supports UTF-8 locales: 如果您使用的是Windows,则可以在CP_UTF8代码页上使用WideCharToMultiByte函数进行转换,因为我不认为Visual Studio C运行时支持UTF-8语言环境:

// Error checking omitted for expository purposes
const wchar_t *wideString = L"たTest!";
size_t len = wcslen(wideString);
size_t maxUtf8len = 4 * len + 1;  // Each wchar_t encodes to a max of 4 bytes
char *utf8String = new char[maxUtf8len];
WideCharToMultiByte(CP_UTF8, 0, wideString, len + 1, utf8String, maxUtf8len, NULL, NULL);
...
// Do stuff with utf8string
...
delete [] utf8String;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM