我有一个JAVA函数,它通过JNI调用C函数。
我的Java功能:
char details= 'd';
char reg_code='r';
char[] reg_chal ={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06} ;
char[] aid = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06};
RegisterInfo regInfo = new RegisterInfo( reg_chal,aid);
res = nativeLib.reg(regInfo, details, reg_code);现在我的C文件包含JNI的东西..其实我发送registerInfo对象到JNI。 Register Info类包含一些char数组。我想将char数组的值赋给我的C结构:reg_info_t:
#define DEBUG_TAG "NDK_NativeLib"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, __VA_ARGS__)
typedef struct{
unsigned char reg_chal[12];
unsigned char aid[12];
}reg_info_t;
JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_reg(JNIEnv *env,jclass obj,jobject info ,jchar details,jchar reg_code)
{
reg_info_t regInfo;
jclass clazz;
jfieldID fid;
jmethodID mid;
jint status =0;
LOGD("NDK:LC: [%s]", "JNI Function call started");
clazz = (*env)->GetObjectClass(env, info);
if (0 == clazz)
{
printf("GetObjectClass returned 0\n");
return(-1);
}
jstring reg_chal= (*env)->NewStringUTF(env,regInfo.reg_chal);
fid = (*env)->GetFieldID(env,clazz,"reg_chal","Ljava/lang/String;");
(*env)->SetObjectField(env,info,fid,reg_chal);
LOGD("NDK:LC: [%s]", reg_chal);
return status;
}RegisterInfo.java
public class RegisterInfo {
private char[] reg_chal ;
private char[] aid;
public RegisterInfo(char[] reg_chal,char[] aid) {
super();
this.reg_chal = reg_chal;
this.aid = aid;
}
}现在,当我运行代码我得到的错误:
09-14 10:14:19.536: WARN/dalvikvm(666): **JNI WARNING: illegal start byte 0xf0**
09-14 10:14:19.536: WARN/dalvikvm(666): string: '�ѯ$ԯ�'
09-14 10:14:19.536: WARN/dalvikvm(666): in Lcom/marakana/NativeLib;.reg (Lcom/marakana/RegisterInfo;CC)I (NewStringUTF)我想我在string,char[]和对象分配之间进行转换时出错。你能帮我么。提前致谢。