c++ 用GetCommandLine() 获取命令行参数出现中文乱码,怎么办?

这个得到的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,网上找GBKtoUTF8
for(;*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->NewString

return r;

}
} // end of extern
#endif

慕妹3146593
浏览 642回答 1
1回答

精慕HU

// 注意这个函数不支持中文,因为没有将GBK编码转UTF,网上找GBKtoUTF8string 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;}
打开App,查看更多内容
随时随地看视频慕课网APP