JNI---uint8_t array转string

#define  SOFTWARE_VSERSION_LEN                   16+1  //加1代表数组末尾追加'\0'
#define  GLXSS_ID_LEN                            8+1  //加1代表数组末尾追加'\0'

typedef struct sensor_product_info{
 uint8_t software_version[SOFTWARE_VSERSION_LEN];
 uint8_t glxss_id[GLXSS_ID_LEN];
}sensor_product_info_t;

转化代码如下:
int HID::sensor_get_product_info(product_info product){
if(mHIDDev == NULL) {
      LOGE("native dev not initialized");
      return HID_NOT_INIT;
  }

  int ret;
  uint8_t data[sizeof(*product)-2];

  ret = libusb_control_transfer(mHIDDev->handle, BM_REQUEST_TYPE, GET_CHIP_INFO, 0,
                                HID_INTERFACE_INDEX, data, sizeof(*product)-2, TIME_OUT);
    LOGE("sensor_get_product_info:ret=%d",ret);
    if (ret == sizeof(*product)-2) {
      for(int i=0;i<16;i++){
          product->software_version[i] = data[i];
       }
        product->software_version[16]='\0';//字符串末尾追加'\0',方便上层char *vdata= (char *) product->software_version直接转化
        for(int i=16;i<24 ;i++) {
           product->glxss_id[i-16] = data[i];
       }
        product->glxss_id[8]='\0';//字符串末尾追加'\0',方便上层 char *gdata=(char*)product->glxss_id直接转化
       return HID_SUCCESS;
  } else {
      return ret;
  }
}



char *vdata= (char *) product->software_version;
char *gdata=(char*)product->glxss_id;
jstring jversion=env->NewStringUTF(vdata);
jstring jglxssid=env->NewStringUTF(gdata);
 
  
注:
转化时,需要手动在末尾添加‘\0’

你可能感兴趣的:(JNI)