JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xfe

JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xfe

在使用Jni的JNIEnv->NewStringUTF的時候拋出了異常"JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xfe "。網上搜索了一下,這個異常是因爲Java虛擬機內部的dalvik/vm/CheckJni.c中的checkUtfString函數拋出的,而且JVM的這個接口明確是不支持四個字節的UTF8字符。所以須要在調用函數以前,對接口傳入的字符串進行過濾,過濾函數以下:html

參考連接 android jni中 utf-8的checkjava

發佈者

 

默默

默默碼農 查看全部由默默發佈的文章 android

 

 

#########################################################################################################函數

newStringUTF出現input is not valid Modified UTF-8錯誤解決辦法

2013年10月12日 ⁄ 綜合 ⁄ 共 883字 ⁄ 字號 ⁄ 評論關閉
 

JNI調用newStringUTF時遇到不認識的字符串就直接出錯退出~~,網上緣由是dalvik/vm/CheckJni.c裏面的checkUtfString函數檢查通不過.找了半天沒找到修正辦法,把這個函數改了下,當作一個修正函數,下面是代碼spa

 

void correctUtfBytes(char* bytes) {
  char three = 0;
   while (*bytes != '\0') {
	   unsigned char utf8 = *(bytes++);
	   three = 0;
	   // Switch on the high four bits.
	   switch (utf8 >> 4) {
	   case 0x00:
	   case 0x01:
	   case 0x02:
	   case 0x03:
	   case 0x04:
	   case 0x05:
	   case 0x06:
	   case 0x07:
		   // Bit pattern 0xxx. No need for any extra bytes.
		   break;
	   case 0x08:
	   case 0x09:
	   case 0x0a:
	   case 0x0b:
	   case 0x0f:
		   /*
			* Bit pattern 10xx or 1111, which are illegal start bytes.
			* Note: 1111 is valid for normal UTF-8, but not the
			* modified UTF-8 used here.
			*/
		   *(bytes-1) = '?';
		   break;
	   case 0x0e:
		   // Bit pattern 1110, so there are two additional bytes.
		   utf8 = *(bytes++);
		   if ((utf8 & 0xc0) != 0x80) {
			   --bytes;
			   *(bytes-1) = '?';
			   break;
		   }
		   three = 1;
		   // Fall through to take care of the final byte.
	   case 0x0c:
	   case 0x0d:
		   // Bit pattern 110x, so there is one additional byte.
		   utf8 = *(bytes++);
		   if ((utf8 & 0xc0) != 0x80) {
			   --bytes;
			   if(three)--bytes;
			   *(bytes-1)='?';
		   }
		   break;
	   }
   }
}http://www.xuebuyuan.com/1240531.htmlhttp://www.mobibrw.com/2016/2859
相關文章
相關標籤/搜索