jni——如何转换有符号与无符号数
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——如何转换有符号与无符号数的更多相关文章
- C++有符号和无符号数的转换
本文转自:http://www.94cto.com/index/Article/content/id/59973.html 1.引例: 今天在做了一道关于有符号数和无符号数相互转换及其左移/右移的问题 ...
- 【C语言学习趣事】_33_关于C语言和C++语言中的取余数(求模)的计算_有符号和无符号数的相互转换问题
最近再次复习C++语言,用的教材是<C++ Primer>这本教材, 看到第二章的时候,里面有个问题困扰了我. 于是想上网查查怎么回事, 结果看了很久都没有得到一个满意的答案. 书上有这么 ...
- c语言中为什么左移不分符号数无符号数,而右移分呢??
因为在C语言标准中,只规定了无符号数的移位操作是采用逻辑移位(即左移.右移都是使用的逻辑左移和逻辑右移).而对于有符号数,其左移操作还是逻辑左移,但右移操作是采用逻辑右移还是算术右移就取决于机器了!( ...
- 关于C语言中不同类型数据进行计算 有符号和无符号数进行计算
float是8个有效位, 做个试验: 输出如下: 上面说明了什么: 1, 18/2.2 是除不尽的, 因为是define,所以没有给ratio变量赋值类型,但是从sizeof输出的结果是8,所以系统默 ...
- signed 与 unsigned 有符号和无符号数
unsigned int a = 0; unsigned int b = -1; // b 为 0xffffffff unsigned int c = a - 1; // c 为 0xffffffff
- C语言-无符号数与有符号数不为人知的秘密
一.无符号数与有符号数 1.计算机中的符号位 数据类型的最高位用于标识数据的符号 -最高位为1,表明这个数为负数 -最高位为0,表明这个数为正数 #include <stdio.h> in ...
- 关于有符号数和无符号数的转换 - C/C++
转载自:http://www.94cto.com/index/Article/content/id/59973.html 1.引例: 今天在做了一道关于有符号数和无符号数相互转换及其左移/右移的问题, ...
- JAVA byte有无符号数的转换
如果你只需要对英文文本的每个字节进行数据处理,则无需考虑有符号数和无符号数的转换问题: 但如果你需要对含有中文的文本进行字节处理,则可能需要考虑有无符号数的转换问题. 以下代码均为Java代码. 1. ...
- C语言中无符号数和有符号数之间的运算
C语言中无符号数和有符号数之间的运算 C语言中有符号数和无符号数进行运算(包括逻辑运算和算术运算)默认会将有符号数看成无符号数进行运算,其中算术运算默认返回无符号数,逻辑运算当然是返回0或1了. un ...
随机推荐
- 暑假集训 || 概率DP
Codeforces 148D 考虑状态转移..https://www.cnblogs.com/kuangbin/archive/2012/10/04/2711184.html题意:原来袋子里有w只白 ...
- 初探node.js
一.定义及优势 定义:Node.js是一个基于 Chrome V8 引擎 的 JavaScript 运行时,它以事件驱动为基础实现了非阻塞模型. 优势:由于Web场景下的大多数任务(静态资源读取.数据 ...
- CSS---基础外部样式表
<link rel="stylesheet" type="text/css" href="style.css" media=" ...
- MySQL数据库安装与Navicat Premium 12 安装与破解
一.文件下载: MySQL:官网:https://www.mysql.com/downloads/(现在最新的是5.7版) 下载路径:"Downloads" ==>> ...
- POJ 3659 Cell phone Network (树的最小点覆盖, 树形DP)
题意: 给定一棵树,每个点可以覆盖自己和相邻的点, 求最少要多少个点覆盖图 #include <cstdio> #include <cstring> #include < ...
- 调用hcm接口同步员工数据更新员工信息没有同步到bdm
原来是更新的时候,baseEmployeeEntity的id为空,这时候需要把原先的baseEmployeeEntity1的id赋值给baseEmployeeEntity,问题解决
- Python中input()和raw_input()函数的区别
问题:在Python2.7中使用 input() 函数会出现 “NameError: Name ”***“ is not defined 的错误 解决: 使用raw_input() 函数,在Pytho ...
- hdu 1421经典dp
#include<stdio.h> #include<stdlib.h> #define N 2001 #define inf 0x3fffffff int a[N],dp[N ...
- 2016 Multi-University Training Contest 1 solutions BY HIT
首先向大家表示抱歉,因为这套题是去年出的,中间间隔时间太长,今年又临时准备仓促, 所以部分题目出现了一些问题,非常抱歉. Abandoned country 首先注意到任意两条边的边权是不一样的,由此 ...
- 【收藏】SSH原理与运用
http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html http://www.ruanyifeng.com/blog/2011/12/ ...