CString to LPWSTR

本文介绍了一种使用CString进行字符串转换的简便方法,通过将LPCTSTR类型的str转换为LPWSTR类型,从而实现不同字符串类型间的高效转换。

简便方法:

CString str;

(LPWSTR)(LPCTSTR)str;

转载于:https://www.cnblogs.com/davie/archive/2012/05/17/2506217.html

bool MainWindow::GetPanShiLink(QString str, QVector<QVector<data2> > &datas) { // QString strIP; // QSqlDatas sql; return true; } static std::string ConvertCStringToUTF8(CString strValue) { std::wstring wbuffer; wbuffer.assign(strValue.GetString(), strValue.GetLength()); /* 获取转换后长度 */ int length = WideCharToMultiByte(CP_UTF8, 0, wbuffer.data(), wbuffer.size(), NULL, 0, NULL, NULL); /* 获取转换后内容 */ std::string buffer; buffer.resize(length); WideCharToMultiByte(CP_UTF8, 0, strValue, -1, (LPSTR)(buffer.data()), length, NULL, NULL); return(buffer); } size_t write_data(char *strdata, size_t size, size_t nmemb, std::string *strRetData) { if (strRetData == NULL) { return 0; } CString csResultData; //csResultData.Format(L"%s", strdata); csResultData = Utf8ToAnsi(strdata);// .Format(L"%s", strdata); csReturn = csReturn + csResultData; size_t len = size * nmemb; strRetData->append(strdata, len); return len; } CString SpecialCharacteristic::JsonTOCString(cJSON * json) { if (!json) { return L""; } if (json->type == cJSON_Number) { if (json->valueint == json->valuedouble) //int { int ival = json->valueint; CString strNumber; strNumber.Format(_T("%d"), ival); return strNumber; } else//double { double dval = json->valuedouble; CString strNumber; strNumber.Format(_T("%.2f"), dval); return strNumber; } } else if (json->type == cJSON_String) { string value = json->valuestring; CString str; CStringW unicodeStr; int len = MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, NULL, 0); if (len > 0) { LPWSTR buf = unicodeStr.GetBuffer(len); MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, buf, len); str.Format(L"%ls", buf); unicodeStr.ReleaseBuffer(); } return str; /////////////////////////////////////////////// //std::string value = json->valuestring; //int len = MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, NULL, 0); //wchar_t* wstr = new wchar_t[len]; //MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, wstr, len); //int len2 = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL); //char* str = new char[len2]; //WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len2, NULL, NULL); //CString cstr(str);//特殊符号会给改成其它 //delete[] wstr; //delete[] str; //////////////////////////////////////////////// /*string value = json->valuestring; LPCWSTR unicodeStr1 = CA2T(value.c_str(), CP_UTF8);//字段过长会乱码 CString cstr(unicodeStr1);*/ } else if (json->type == cJSON_Array) { CString strResult; for (int i = 0; i < cJSON_GetArraySize(json); i++) { cJSON* item = cJSON_GetArrayItem(json, i); string value = cJSON_Print(item); CString str; CStringW unicodeStr; int len = MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, NULL, 0); if (len > 0) { LPWSTR buf = unicodeStr.GetBuffer(len); MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, buf, len); str.Format(L"%ls", buf); unicodeStr.ReleaseBuffer(); } str.Replace(L"\"", L""); strResult += str; if (i != cJSON_GetArraySize(json) - 1) { strResult += L","; } } return strResult; } else if (json->type == cJSON_False) { return L"false"; } else if (json->type == cJSON_True) { return L"true"; } else { return L""; } } BOOL SpecialCharacteristic::InitiateReviewPanShiApi(CString Body, CString & errMsg, CString& Sename) { vector<vector<data2>> datas{}; if (!GetPanShiLink(L"磐石提交产品特性变更申请接口", datas)) {//提交产品特性申请https://compliance.his.huawei.com/SpecialManagement/external/product/character/submit AfxMessageBox(L"数据库链接失败,请联系管理员"); return FALSE; } CString strDatas = (CString)datas[0][0].LineValue; CString strAPIURL = strDatas; CURL *curl; CString strHtml; CString strInfo; strInfo.Format(L"{\"url\":\"%s\",\"jsonStr\":%s}", strAPIURL, Body); curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); USES_CONVERSION;//声明标识符 //char *c_url = T2A(strAPIURL); curl_slist *head = NULL; head = curl_slist_append(head, "Content-Type: application/json"); CURLcode res; curl_easy_setopt(curl, CURLOPT_URL, "http://10.11.1.10:1024/user/postRequest");//地址 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, head); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); //设置请求的超时时间 curl_easy_setopt(curl, CURLOPT_POST, 1L);//POST请求 string jsonInfo = ConvertCStringToUTF8(strInfo); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonInfo.c_str()); // 要发送的数据 std::string strdata; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);//设置回调函数,用于处理返回的数据 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strdata); csReturn = ""; res = curl_easy_perform(curl); CString m_ErrMsg = L"获取磐石数据失败,请联系管理员"; if (res != CURLE_OK) { errMsg = m_ErrMsg; return FALSE; } curl_easy_cleanup(curl); strHtml = ""; strHtml = csReturn; char *json_str = (char*)(LPCTSTR)strHtml; cJSON *root = cJSON_Parse(strdata.c_str()); if (root == NULL) { errMsg = m_ErrMsg; return FALSE; } cJSON *code = cJSON_GetObjectItem(root, "code"); if (code == NULL) { errMsg = m_ErrMsg; return FALSE; } cJSON *data1 = cJSON_GetObjectItem(root, "data"); if (data1 == NULL) { errMsg = m_ErrMsg; return FALSE; } cJSON *analysisMsg = cJSON_GetObjectItem(root, "analysisMsg"); if (analysisMsg == NULL) { errMsg = m_ErrMsg; return FALSE; } cJSON *msg = cJSON_GetObjectItem(root, "msg"); if (msg == NULL) { errMsg = m_ErrMsg; return FALSE; } CString strCode = JsonTOCString(code); Sename = JsonTOCString(data1);//评审页面 CString stranalysisMsg = JsonTOCString(analysisMsg); CString strMsg = JsonTOCString(msg); if (csReturn.Find(L"服务调用成功") >= 0 || strCode.Find(L"200") >= 0) { return TRUE; } //{"code":99999,"msg":"特性状态非编制中、已驳回、评审不通过,不允许发起评审!", else if (strCode.Find(L"99999") >= 0) { errMsg = strMsg; return FALSE; } else { return FALSE; } } 换成qt5.9.7实现
最新发布
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值