为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C++ C
慕妹3146593 2021-12-02 07:07:12
这个得到的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;
}



查看完整回答
反对 回复 2021-12-06
  • 1 回答
  • 0 关注
  • 527 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信