如何在Kotlin/Native中将const char *转换为KString? [英] How to convert const char* to KString in Kotlin/Native?
问题描述
在C ++文件中,我想将const char *转换为KString,这样我就可以使用
In a C++ file, I want to convert a const char* to KString, so that I can then pass the KString to a Kotlin file using Kotlin/Native.
我相信答案就在于功能
OBJ_GETTER(utf8ToUtf16, const char* rawString, size_t rawStringLength)
我在 KString中找到的 .cpp utf8ToUtf16
来获取KString.感谢您的帮助.
that I found in KString.cpp. But even though I discovered the used define statements in Memory.h, I have not yet managed to properly call the function utf8ToUtf16
from my own C++ file to get a KString. Any help is appreciated.
推荐答案
这取决于您如何与Kotlin代码进行交互.如果使用-produce dynamic
生成动态库,则字符串将自动转换,例如,参见
It depends on how you want to interact with Kotlin code. If you produce dynamic library with -produce dynamic
, then string are converted automatically, see for example https://github.com/JetBrains/kotlin-native/blob/adf8614889e8cf5038a79960aa9651ca7d45e409/samples/python_extension/src/main/c/kotlin_bridge.c#L72.
So no additional magic is required at all. Same with Objective-C strings and -produce framework
. And for other cases, there shall be no need to pass strings C -> Kotlin (callbacks produced with staticCFunction
also do autoconversion).
这篇关于如何在Kotlin/Native中将const char *转换为KString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!