java数据结构默认均为有符号数,而通过jni转换到c/c++层,却不一定是有符号数。

如若在java中存储的即为无符号数,则在jni中可将jbyte直接进行类型转换。

若进行操作,则可在计算时,先将byte&0xff,这样即可转换为32位数据,而后再进行计算。

转换方式如下:

1、jbyteArray转换为unsigned char*

Java

 public class example {
public final static native void set_Foo_array(long jarg0, short[] jarg1);
public final static native short[] get_Foo_array(long jarg0);
}

c++ code:

class Foo {
public:
unsigned char array[];
}; extern "C"{
JNIEXPORT void JNICALL Java_example_set_1Foo_1array(JNIEnv *jenv, jclass jcls, jlong jarg0, jshortArray jarg1) {
Foo *arg0 ;
unsigned char *arg1 ;
int i ;
jshort* jarg1_carray ;
jsize jarg1_len = jenv->GetArrayLength(jarg1) ; arg0 = *(Foo **)&jarg0;
jarg1_carray = jenv->GetShortArrayElements(jarg1, );
arg1 = (unsigned char *) malloc(jarg1_len * sizeof(unsigned char ));
for(i=; i<jarg1_len; i++)
arg1[i] = (unsigned char )jarg1_carray;
{
int i;
for (i=; i<; i++)
arg0->array[i] = arg1[i];
}
jenv->ReleaseShortArrayElements(jarg1, jarg1_carray, );
free(arg1);
}} extern "C"{
JNIEXPORT jshortArray JNICALL Java_example_get_1Foo_1array(JNIEnv *jenv, jclass jcls, jlong jarg0) {
jshortArray jresult = ;
Foo *arg0 ;
unsigned char *result ;
jshort* jnitype_ptr = ;
int k ; arg0 = *(Foo **)&jarg0;
result = (unsigned char *)(unsigned char *) (arg0->array);
jresult = jenv->NewShortArray();
jnitype_ptr = jenv->GetShortArrayElements(jresult, );
for (k=; k<; k++)
jnitype_ptr[k] = (jshort)result[k];
jenv->ReleaseShortArrayElements(jresult, jnitype_ptr, );
return jresult;
}} The code comes from the output of swig (www.swig.org) - a tool which generated the JNI and Java code given the C++ class definition above.

2、char*转jbyte

 static .. jvm; // ref to jvm
static jobject printAPI; // the static class ref
static jmethodID loadBuffer; // the static method ref void loadImage(int x, int y, int width, int height, char* pixels, int maxWidth, int maxHeight){
JNIEnv* env;
jint result = (*jvm)->GetEnv(jvm, &env, JNI_VERSION_1_2);
if(result < || env == NULL){
fprintf(stderr, "Error finding JNI environment\n");
}else{
int i, size = width*height*;
jbyteArray rgb; // the byte array
jbyte* v; // array of jbytes for transfer operation /* Transfer char* to jbyteArray */
rgb = (*env)->NewByteArray(env, width * height *);
v = malloc( (width * height *) * sizeof(jbyte));
if( !rgb || !v ){
fprintf(stderr, "Error allocating memory in loadImage(..)");
return;
}
for(i=;i<size; ++i) v[i] = (jbyte) pixels; (*env)->SetByteArrayRegion(env, rgb, , size, v); /* send pixel dump to gui */ (*env)->CallStaticIntMethod(env, printAPI, loadBuffer, x, y, width, height, rgb); } } the method on the java side is
static void loadByteArray( int x, int y, int w, int h, byte[] rgb ){..}
Hope it helps.

jni——如何转换有符号与无符号数的更多相关文章

  1. C++有符号和无符号数的转换

    本文转自:http://www.94cto.com/index/Article/content/id/59973.html 1.引例: 今天在做了一道关于有符号数和无符号数相互转换及其左移/右移的问题 ...

  2. 【C语言学习趣事】_33_关于C语言和C++语言中的取余数(求模)的计算_有符号和无符号数的相互转换问题

    最近再次复习C++语言,用的教材是<C++ Primer>这本教材, 看到第二章的时候,里面有个问题困扰了我. 于是想上网查查怎么回事, 结果看了很久都没有得到一个满意的答案. 书上有这么 ...

  3. c语言中为什么左移不分符号数无符号数,而右移分呢??

    因为在C语言标准中,只规定了无符号数的移位操作是采用逻辑移位(即左移.右移都是使用的逻辑左移和逻辑右移).而对于有符号数,其左移操作还是逻辑左移,但右移操作是采用逻辑右移还是算术右移就取决于机器了!( ...

  4. 关于C语言中不同类型数据进行计算 有符号和无符号数进行计算

    float是8个有效位, 做个试验: 输出如下: 上面说明了什么: 1, 18/2.2 是除不尽的, 因为是define,所以没有给ratio变量赋值类型,但是从sizeof输出的结果是8,所以系统默 ...

  5. signed 与 unsigned 有符号和无符号数

    unsigned int a = 0; unsigned int b = -1; // b 为 0xffffffff unsigned int c = a - 1; // c 为 0xffffffff

  6. C语言-无符号数与有符号数不为人知的秘密

    一.无符号数与有符号数 1.计算机中的符号位 数据类型的最高位用于标识数据的符号 -最高位为1,表明这个数为负数 -最高位为0,表明这个数为正数 #include <stdio.h> in ...

  7. 关于有符号数和无符号数的转换 - C/C++

    转载自:http://www.94cto.com/index/Article/content/id/59973.html 1.引例: 今天在做了一道关于有符号数和无符号数相互转换及其左移/右移的问题, ...

  8. JAVA byte有无符号数的转换

    如果你只需要对英文文本的每个字节进行数据处理,则无需考虑有符号数和无符号数的转换问题: 但如果你需要对含有中文的文本进行字节处理,则可能需要考虑有无符号数的转换问题. 以下代码均为Java代码. 1. ...

  9. C语言中无符号数和有符号数之间的运算

    C语言中无符号数和有符号数之间的运算 C语言中有符号数和无符号数进行运算(包括逻辑运算和算术运算)默认会将有符号数看成无符号数进行运算,其中算术运算默认返回无符号数,逻辑运算当然是返回0或1了. un ...

随机推荐

  1. 通俗理解 模糊自适应PID

    模糊自适应PID算法就是在经典的PID的基础上添加模糊控制规则库,建立这个库的目的就是算法能够自己来进行改变P.I.D的值. 就拿温度的上升过程控制来说,刚开始的时候,希望温度能够快速的升到终点温度, ...

  2. Eaton Char-Lynn Motor : Performance Of Small Displacement Motors

    The small-displacement supercharged motor replaces the large-displacement motor with the speed of li ...

  3. tensorflow-gpu install check

    https://gist.github.com/mrry/ee5dbcfdd045fa48a27d56664411d41c#file-tensorflow_self_check-py-L16

  4. 「 Luogu P2574 」 XOR的艺术——线段树

    # 解题思路 这题不难,但是原谅我一开始的傻逼想法,一会儿再给大家透露透露. 先说怎么做这题. 显然对于 $0$ 和 $1$ 来说,异或无非也就只有两种变化 异或了奇数次,$0$ 就会变成 $1$,$ ...

  5. Django生成二维码

    1. 安装 pip install qrcode 安装Image包 pip install Image 1.1 在代码中使用 import qrcode img = qrcode.make('输入一个 ...

  6. webpack 之 loader

    loader简介 loader在webpack.config.js中进行配置,配置之后,会自动检测打包过程中引入的文件(import或require),通过test成功匹配被引入的文件名后,会对文件内 ...

  7. tomcat7使用dbcp连接池遇到的坑

    项目部署在tomcat后每隔一段时间便会报错 Cause: java.sql.SQLException: Could not retrieve transation read-only status ...

  8. Feedback on Ch5 paper based on CFD-RANS

    It is encouraging that you took the initiative to write this journal manuscript, but it needs a lot ...

  9. LeetCode(237)Delete Node in a Linked List

    题目 Write a function to delete a node (except the tail) in a singly linked list, given only access to ...

  10. [第一波模拟\day3\T2]{独立集}(bubble.cpp)

    [问题描述] 有一天,一个名叫顺旺基的程序员从石头里诞生了.又有一天,他学会了冒泡排序和独立集.在一个图里,独立集就是一个点集,满足任意两个点之间没有边.于是他就想把这两个东西结合在一起.众所周知,独 ...