Convertir tipos JNI a Tipos nativos


Aunque hay documentación sobre convertir un jstring a una cadena nativa (string nativeString = env->GetStringUTFChars(jStringVariable, NULL);) No puedo encontrar un ejemplo que convierta un jboolean a un bool o un jint a un int.

Puede alguien sugerir cómo se logra esto?

Author: Yuchen Zhong, 2011-06-10

6 answers

Solo necesita convertir jint a int usando moldes de estilo C. Lo mismo para jboolean para bool (si está utilizando el tipo C99 bool) o para uint8_t (si está utilizando tipos std int) o para unsigned char.

Abre $NDK_ROOT/platforms/android-8/arch-arm/usr/include/jni.h y verás jint, jboolean etc son solo typedef s.

 47
Author: Gregory Pakosz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-10 15:45:18

Para lanzar un jboolean (que solo puede contener los valores JNI_FALSE o JNI_TRUE) a un bool nativo usaría algo como esto:

(bool)(jboolean == JNI_TRUE)

Si tal vez el jboolean no proviene de la JVM, entonces las pruebas para jboolean != JNI_FALSE podrían considerarse más seguras.

 23
Author: Guillaume Bréhier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-15 03:33:30

El mismo problema–solucionado. En mi caso estoy usando openFrameworks, así que no se si esto se aplica a proyectos que no son de openFrameworks (no he probado). Sin embargo, parece que los dos primeros argumentos en una función externa son siempre "env" y "thiz" y estos necesitan ser definidos explícitamente para cada nueva función externa.

extern "C"{

// casts the variable properly
void Java_com_package_JavaClass_someFunction( JNIEnv*  env, jobject  thiz, jboolean yourBool ){
    myTestApp->someFunction( (bool) yourBool );
}

// "yourBool" will always be "1" because its taking the spot of "thiz" which is not null
void Java_com_package_JavaClass_someFunction( JNIEnv*  env, jboolean yourBool ){
    myTestApp->someFunction( (bool) yourBool );
}

// "yourBool" will always be "1" because its taking the spot of "env" which is not null
void Java_com_package_JavaClass_someFunction( jboolean yourBool ){
    myTestApp->someFunction( (bool) yourBool );
}


}
 3
Author: Steven,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-14 21:38:07
 2
Author: John,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-10 15:45:21

La salida impar es jchar. Se define como unsigned short, y dependiendo de su configuración de compilación, que puede o no puede ser equivalente a wchar_t. Dependiendo de su plataforma subyacente, puede ser mejor trabajar con cadenas UTF8. Por lo menos esos son bit a bit equivalentes a ASCII para el subconjunto ASCII de caracteres.

En Windows y Mac OS/Cocoa, sin embargo, la representación nativa de cadenas anchas es exactamente unsigned short. Las cadenas Java encajan naturalmente en eso.

 1
Author: Seva Alekseyev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-17 14:04:46

Si solo quieres usarlo en if(..) declaración, esto está trabajando para mí sin conversión:

if (jboolean == true) {
  return true;
} else {
  return false;
}
 1
Author: zajac.m2,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-10 03:47:23