gpt4 book ai didi

c++ - 使用 JNI 和 C++ 将 Unicode const char* 转换为 JString

转载 作者:行者123 更新时间:2023-12-02 02:44:45 25 4
gpt4 key购买 nike

简单的问题。如何使用 JNI 和 C++ 从 unicode const char* 中获取 jstring?

这是我的问题,以及我已经尝试过的:

const char* value = (some value from server);
(*env)->NewStringUTF(value);

这里的问题是 NewStringUTF 返回一个 UTF 字符串,并且它不喜欢某些非 UTF8 字符(有点明显,但值得简单尝试一下)。

尝试 2,使用 NewString:

const char* value = (some value from server);
(*env)->NewString(value, strlen(value));

虽然 NewString 接受并返回 unicode 字符串,但 strlen(value) 方法不起作用,因为它需要 jsize 参数,而不仅仅是一个好的 ol' size_t 或长度。

我们如何获得 jsize?根据(非常非常少量的)文档和在线示例,您可以从 jIntArray 中获取 jsize。我找不到有关如何将 const char* 转换为某种 jarray 的信息,无论如何,这可能是一个坏主意。

另一个选择是从 size_t 中的 int 中获取 jsize,我也没有成功。

有人遇到过这个问题,或者有关于如何解决这个问题的建议吗?看来 jsize 是我在 unicode 转换中缺少的关键。另外,我正在使用 JNI 和 Android NDK,以防它对任何人有帮助。

谢谢。

编辑我刚刚意识到 NewString 也需要 jchar*,所以它的签名是 (jchar*, jsize)。这意味着即使使用 jsize,const char* 也无法编译。

编辑2这是使用 NewStringUTF 方法时在运行时引发的异常。这与 @fadden 正在谈论的内容相关:

JNI WARNING: NewStringUTF input is not valid Modified UTF-8: illegal start byte 0xb7 03string: ' : Method(d6, us-dev1-api, 0), , 訩�x�m�P)

最佳答案

如错误消息所示,您的 char* 不是有效的 Modifed-utf8,因此 JVM 中止。

您有两种方法可以避免它们。

  1. 检查 char* 内容以避免崩溃。

android ART check_jni.cc中的检查逻辑如下 https://android.googlesource.com/platform/art/+/35e827a/runtime/check_jni.cc#1273

jstring toJString(JNIEnv* env, const char* bytes) {
const char* error = nullptr;
auto utf8 = CheckUtfBytes(bytes, &error);
if (error) {
std::ostringstream msg;
msg << error << " 0x" << std::hex << static_cast<int>(utf8);
throw std::system_error(-1, std::generic_category(), msg.str());
} else {
return env->NewStringUTF(bytes);
}

这样,您始终会获得有效的 jstring

  • 使用 String 构造函数从 jbyteArray 构建。
  • jstring toJString(JNIEnv *env, const char *pat) {
    int len = strlen(pat);
    jbyteArray bytes = env->NewByteArray(len);
    env->SetByteArrayRegion(bytes, 0, len, (jbyte *) pat);
    jstring encoding = env->NewStringUTF("utf-8");
    jstring jstr = (jstring) env->NewObject(java_lang_String_class,
    java_lang_String_init, bytes, encoding);
    env->DeleteLocalRef(encoding);
    env->DeleteLocalRef(bytes);
    return jstr;
    }

    这样,你只是避免了崩溃,但字符串可能仍然无效,并且你复制了两次内存,这表现得很糟糕。

    加上代码:

    inline bool checkUtfBytes(const char* bytes) {
    while (*bytes != '\0') {
    const uint8_t* utf8 = reinterpret_cast<const uint8_t*>(bytes++);
    // Switch on the high four bits.
    switch (*utf8 >> 4) {
    case 0x00:
    case 0x01:
    case 0x02:
    case 0x03:
    case 0x04:
    case 0x05:
    case 0x06:
    case 0x07:
    // Bit pattern 0xxx. No need for any extra bytes.
    break;
    case 0x08:
    case 0x09:
    case 0x0a:
    case 0x0b:
    // Bit patterns 10xx, which are illegal start bytes.
    return false;
    case 0x0f:
    // Bit pattern 1111, which might be the start of a 4 byte sequence.
    if ((*utf8 & 0x08) == 0) {
    // Bit pattern 1111 0xxx, which is the start of a 4 byte sequence.
    // We consume one continuation byte here, and fall through to consume two more.
    utf8 = reinterpret_cast<const uint8_t*>(bytes++);
    if ((*utf8 & 0xc0) != 0x80) {
    return false;
    }
    } else {
    return false;
    }
    // Fall through to the cases below to consume two more continuation bytes.
    case 0x0e:
    // Bit pattern 1110, so there are two additional bytes.
    utf8 = reinterpret_cast<const uint8_t*>(bytes++);
    if ((*utf8 & 0xc0) != 0x80) {
    return false;
    }
    // Fall through to consume one more continuation byte.
    case 0x0c:
    case 0x0d:
    // Bit pattern 110x, so there is one additional byte.
    utf8 = reinterpret_cast<const uint8_t*>(bytes++);
    if ((*utf8 & 0xc0) != 0x80) {
    return false;
    }
    break;
    }
    }
    return true;
    }

    关于c++ - 使用 JNI 和 C++ 将 Unicode const char* 转换为 JString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28990038/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com