JNI:将unsigned int转换为jint [英] JNI: converting unsigned int to jint

查看:235
本文介绍了JNI:将unsigned int转换为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆