日期:2014-05-20 浏览次数:20765 次
JNIEXPORT jstring JNICALL Java_com_szzc_jni_JniApi_getCardSerial
(JNIEnv * env, jobject obj, jcharArray js){
hModule=LoadLibrary("OUR_MIFARE.dll");
gcnum = (getCardNum)GetProcAddress(hModule, "piccrequest");
//.....
unsigned char * str = (unsigned char *)env->GetCharArrayElements(js,NULL);
unsigned char cardNum = gcnum(str);
printf("卡列号:\n");
unsigned char* p = str;
while(*p!='\0'){
printf("%x\n",*p);
p++;
}
//定义java String类 strClass
jclass strClass = env->FindClass("Ljava/lang/String;");
//获取java String类方法String(byte[],String)的构造器,用于将本地byte[]数组转换为一个新String
jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
//建立byte数组
jbyteArray bytes = env->NewByteArray((jsize)strlen((const char *)str));
//将char* 转换为byte数组
env->SetByteArrayRegion(bytes, 0, (jsize)strlen((const char *)str), (jbyte*)(const char *)str);
//设置String, 保存语言类型,用于byte数组转换至String时的参数
jstring encoding = env->NewStringUTF("utf-8");
//将byte数组转换为java String,并输出
jstring status = (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
cout << status <<endl;
return status;
// return (jstring)cardNum;
}
unsigned char devicenumber[4];
devicenumber[0]=7;
devicenumber[1]=111;
devicenumber[2]=218;
devicenumber[3]=64;
jstring deviceNum = env->NewStringUTF((const char*)devicenumber);
return deviceNum;
s = new JniApi().getDeviceNo(test);
System.out.println(s);
String str = new String(s.getBytes("UTF-8"));
System.out.println(str);
unsigned cahr * test="1A2B3C4D";都不会乱码。、这个倒底怎么回事?没人知道吗