package com.itheima.charencode;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText; public class MainActivity extends Activity { private EditText et;
static{
System.loadLibrary("hello");//导入类库
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et); } public void click1(View v){
String text = et.getText().toString();
et.setText(encode(text, text.length()));
}
public void click2(View v){
String text = et.getText().toString();
et.setText(decode(text, text.length()));
} public native String encode(String text, int length);
public native String decode(String text, int length);
}
#include <jni.h>
#include <string.h>
//把java的字符串转换成c的字符串
char* Jstring2CStr(JNIEnv* env, jstring jstr)
{
char* rtn = NULL;
jclass clsstring = (*env)->FindClass(env,"java/lang/String");
jstring strencode = (*env)->NewStringUTF(env,"GB2312");
jmethodID mid = (*env)->GetMethodID(env,clsstring, "getBytes", "(Ljava/lang/String;)[B");
jbyteArray barr= (jbyteArray)(*env)->CallObjectMethod(env,jstr,mid,strencode); // String .getByte("GB2312");
jsize alen = (*env)->GetArrayLength(env,barr);
jbyte* ba = (*env)->GetByteArrayElements(env,barr,JNI_FALSE);
if(alen > )
{
rtn = (char*)malloc(alen+); //"\0"
memcpy(rtn,ba,alen);
rtn[alen]=;
}
(*env)->ReleaseByteArrayElements(env,barr,ba,); //
return rtn;
} JNIEXPORT jstring JNICALL Java_com_itheima_charencode_MainActivity_encode
(JNIEnv * env, jobject obj, jstring jstr, jint lenght){
char* cstr = Jstring2CStr(env, jstr);
int i;
for(i = ;i < lenght; i++){
*(cstr + i) += ;
}
return (*env)->NewStringUTF(env, cstr);
} /*
* Class: com_itheima_charencode_MainActivity
* Method: decode
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_itheima_charencode_MainActivity_decode
(JNIEnv * env, jobject obj, jstring jstr, jint lenght){
char* cstr = Jstring2CStr(env, jstr);
int i;
for(i = ;i < lenght; i++){
*(cstr + i) -= ;
}
return (*env)->NewStringUTF(env, cstr);
}

数组处理:

package com.itheima.array;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { static{
System.loadLibrary("hello");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} int[] arr = {,,,,}; public void click(View v){
arrayEncode(arr);
for (int i : arr) {//上面已经对数组内存进行了改变,所以此处是修改后的数组
System.out.println(i);
}
} public native void arrayEncode(int[] arr);
}
/*
hello.c文件: #include <jni.h> JNIEXPORT void JNICALL Java_com_itheima_array_MainActivity_arrayEncode
(JNIEnv * env, jobject obj, jintArray jintarr){//jintArray是整型数组,jni中所有的数组都是指针类型
//拿到整型数组的长度以及第0个元素的地址
//jsize (*GetArrayLength)(JNIEnv*, jarray);jsize就是int类型(jni.h文件都有定义)
int length = (*env)->GetArrayLength(env, jintarr);
//jint* (*GetIntArrayElements)(JNIEnv*, jintArray, jboolean*);jint就是int类型
int* arrp = (*env)->GetIntArrayElements(env, jintarr, 0);//得到第0个元素的地址 int i;
for(i = 0;i < length; i++){//数组是连续的内存空间,有了数组长度和数组第0个元素地址就相当于拿到了所有数组。
*(arrp + i) += 10;//每个元素加10
}
}*/

android112 jni 把java的字符串转换成c的字符串,数组处理的更多相关文章

  1. 剑指Offer 49. 把字符串转换成整数 (字符串)

    题目描述 将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一 ...

  2. 把json格式的字符串转换成javascript对象或数组

      第一种 JSON.parse(jsonString) 第二种 eval("("+jsonString+")") 第三种 var obj=(function ...

  3. js字符串转换成数字与数字转换成字符串的实现方法

    转载:点击查看地址 js字符串转换成数字 将字符串转换成数字,得用到parseInt函数.parseInt(string) : 函数从string的开始解析,返回一个整数. 举例:parseInt(' ...

  4. js字符串转换成数字,数字转换成字符串

    转自网络,忘记出处了. js字符串转换成数字 将字符串转换成数字,得用到parseInt函数. parseInt(string) : 函数从string的开始解析,返回一个整数. 举例:parseIn ...

  5. Java—JSON串转换成实体Bean对象模板

    介绍 模板需求说明   开发中经常遇到前端传递过来的JSON串的转换,后端需要解析成对象,有解析成List的,也有解析成Map的. 依赖 <dependency> <groupId& ...

  6. php将一个字符串转变成键值对数组的效率问题

    有这样一种需求,将形式为"TranAbbr=IPER|AcqSsn=000000073601|MercDtTm=20090615144037"的字符串转换成如下格式的数组: Arr ...

  7. Java将ip字符串转换成整数的代码

    下面代码是关于Java将ip字符串转换成整数的代码,希望对各位有较大用途. public class IpUtil { public static int Ip2Int(String strIp){ ...

  8. 【Java】 剑指offer(67) 把字符串转换成整数

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 请你写一个函数StrToInt,实现把字符串转换成整数这个功能 ...

  9. 转换成json字符串,与json字符串转换成java类型都要先转换成json对象

    转换成json字符串,与json字符串转换成java类型都要先转换成json对象

随机推荐

  1. 图像色彩空间YUV和RGB的差别

    http://blog.csdn.net/scg881008/article/details/7168637 假如是200万像素的sensor,是不是RGB一个pixel是2M,YUV是1M? 首先, ...

  2. 158. Read N Characters Given Read4 II - Call multiple times

    题目: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the ...

  3. Python sh库学习 上篇

    官方文档有句话"allows you to call any program",并且:helps you write shell scripts in Python by givi ...

  4. tomcat docBase 和 path

    <Context docBase="zjzc-web-api" path="/api" reloadable="false"/> ...

  5. 结构体dict_field_t

    typedef struct dict_field_struct dict_field_t; typedef struct dict_field_struct dict_field_t; /** Da ...

  6. sql - and - or

    sql - and SQL AND links together two or more conditional statements for increased filtering when run ...

  7. ASP.NET操作Word的IIS权限配置

    ASP.NET账号在默认情况下是没有权限操作Microsoft Office对象的,如果不进行权限的配置,代码会抛出类似以下的异常: 检索 COM 类工厂中 CLSID 为 {00024500-000 ...

  8. htmlparser使用例子(全) 转载

    1.import java.net.URL;  2.  3.import junit.framework.TestCase;  4.  5.import org.apache.log4j.Logger ...

  9. lightoj 1005

    组合数学,ans = C(n,k)*A(n,k). #include<cstdio> #include<string> #include<cstring> #inc ...

  10. Codeforces 14D

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; const i ...