JNI:将unsigned int转换为jint [英] JNI: converting unsigned int to jint
问题描述
如何将unsigned int转换为jint?我是否必须完全转换它,还是可以不做任何特殊处理就将其退回?这基本上是我现在的代码,但是由于我尚未在本地设置JNI,因此我无法对其进行测试.
How do I convert an unsigned int to jint? Do I have to convert it at all, or can I just return it without any special treatment? This is basically my code right now, but I can't test it, as I haven't setup JNI locally.
JNIEXPORT jint JNICALL
Java_test_test(JNIEnv* env, jobject obj, jlong ptr)
{
MyObject* m = (MyObject*) ptr;
unsigned int i = m->get();
return i;
}
推荐答案
在一般情况下,jint
等同于int
,因此可以容纳unsigned int
值的一半.转换将以静默方式进行,但是如果jint
值为负或unsigned int
值大于jint
可以容纳的最大值,则结果将不是您期望的.
In the general case, jint
is equivalent to int
, and so can hold about half the values of unsigned int
. Conversion will work silently, but if a jint
value is negative or if an unsigned int
value is larger than the maximum value a jint
can hold, the result will not be what you are expecting.
这篇关于JNI:将unsigned int转换为jint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!