這個得到的line字符串只要是中文就是亂碼 該怎么弄呀?代碼程序不是我寫的extern "C" {JNIEXPORT jstring JNICALL Java_TXT_TextDemo_GetPath(JNIEnv * v, jclass TestNative){jstring r = NULL;int i = 0;char *c = new char[100];char line[260];char *utf;LPWSTR p = GetCommandLine();wchar_t *cur = p;char *des = line;unsigned int x = 0;// convert wchar_t* to char*// 注意這個函數不支持中文,因為沒有將GBK編碼轉UTF,網上找GBKtoUTF8for(;*cur!=0;cur++) {x = (unsigned int)*cur;*des = (char)(x%256); des++;if(x>=256) {*des = (char)(x/256); des++;}}*des = 0;r=v->NewStringUTF(line);//r = v->NewStringreturn r;}} // end of extern#endif
1 回答

精慕HU
TA貢獻1845條經驗 獲得超8個贊
// 注意這個函數不支持中文,因為沒有將GBK編碼轉UTF,網上找GBKtoUTF8
string GBKToUTF8(const std::string& strGBK)
{
string strOutUTF8 = "";
WCHAR * str1;
int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
str1 = new WCHAR[n];
MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
char * str2 = new char[n];
WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
strOutUTF8 = str2;
delete[]str1;
str1 = NULL;
delete[]str2;
str2 = NULL;
return strOutUTF8;
}
- 1 回答
- 0 關注
- 725 瀏覽
添加回答
舉報
0/150
提交
取消