[英]Get C++ wchar_t into Flash, via Lua
我目前正在使用C ++開發應用程序,該應用程序與Lua關聯,並與Flash(按此順序)關聯。 目前,我的目標是通過Lua將wchar_t
從C ++導入Flash。 對於我如何實現這一目標,我將不勝感激!
如果需要其他任何信息,請詢問,我會盡力提供
我嘗試過的
據我了解,Lua不喜歡Unicode,但是它仍然應該能夠從我的C ++應用程序接收字節字符串。 我想必須有一種方法可以將這些字節傳遞給Flash,然后呈現出我想要的Unicode。 所以到目前為止我已經做了:
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:
盧阿:
theString = FunctionThatGetsWCharFromCpp();
flash.ShowString(theString);
閃:
function ShowString(theString:String)
{
myTextField.text = theString;
}
現在,這里的結果是myTextField
僅顯示“ T”。 這對我來說很有意義。 從wchar_t
到char
最終會用一些零填充char
,特別是因為“ T”並沒有真正利用wchar_t
兩個字節。 快速瀏覽一下文檔產生的結果:
lua_pushstring
該字符串不能包含嵌入式零。 假定以第一個零結束。
所以我進行了一些測試:
C ++:
//prefixing with a Japanese character
//which will use both bytes of the wchar_t
const wchar_t *text = L"たTest!";
Flash文本框現在顯示為:“ _ 0T”,3個字符。 完全有意義,日語字符+ T的2個字節,然后終止。
我了解發生了什么,但是我仍然不確定如何解決此問題。 我真的不確定要搜索什么。 我是否可以使用特定的Lua函數將一堆字節從C ++傳遞到Lua(我讀過某個地方經常使用lua_pushlstring
,但它也以第一個零終止)? 是否有Flash數據類型可以接受這些字節,然后需要進行某種轉換才能將它們轉換為可讀的多字節字符串? 還是這真的不可能?
注意:
我不太熟悉Unicode和代碼頁等,所以我不太確定是否還有步驟需要在Flash中指定正確的編碼,以便獲得正確的輸出-但是當我到達那座橋時,我很高興過橋,但是如果有人在這里也有任何見識,那就太好了!
我不知道這是否行得通,但是我建議嘗試使用UTF-8 。 以UTF-8編碼的字符串中沒有任何嵌入的零,因此Lua應該能夠處理它,而Flash也應該能夠處理它,具體取決於語言的接口方式。
這是一種使用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;
如果您使用的是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.