【NX二次开发】常用类型转换

//类型转换 string转NXString

NXString nxsTemp(selectedPart.strTemp.c_str());

//类型转换 NXString转string

//类型转换 vector<string>转vector<NXString>
//类型转换 vector<NXString>转vector<string>

 

 

	/**
	 * @brief Unicode to ANSIT
	*/
	static std::string UnicodeToANSI(const wstring& wstr) {
		int nTextLen = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
		if (nTextLen<0) return std::string("");
		char *pszDst = new char[nTextLen +1];
		if (pszDst == NULL) return std::string("");
		memset((void*)pszDst, 0, sizeof(char)*(nTextLen + 1));
		::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, pszDst, nTextLen, NULL, NULL);
		std::string strTemp(pszDst);
		delete[] pszDst;
		return strTemp;
	}

	/**
	* @brief Unicode to UTF-8
	*/
	static std::string UnicodeToUTF8(const wstring& str) {
		int nTextLen = ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
		if (nTextLen<0) return std::string("");
		char* pElementText = new char[nTextLen + 1];
		if (pElementText == NULL) return std::string("");
		memset((void*)pElementText, 0, sizeof(char)*(nTextLen + 1));
		::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, pElementText, nTextLen, NULL, NULL);
		std::string strTemp(pElementText);
		delete[] pElementText;
		return strTemp;
	}

	/**
	 * @brief ANSIT to Unicode
	*/
	static std::wstring ANSIToUnicode(const string& str) {
		int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
		if (unicodeLen<0) return std::wstring(L"");
		wchar_t* pUnicode = new wchar_t[unicodeLen + 1];
		if (pUnicode == NULL) return std::wstring(L"");
		memset(pUnicode, 0, sizeof(wchar_t)*(unicodeLen + 1));
		::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
		std::wstring ret = (wchar_t*)pUnicode;
		delete pUnicode;
		return ret;
	}

	/**
	 * @brief UTF-8 to Unicode
	*/
	static std::wstring UTF8ToUnicode(const string& str) {
		int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
		if (unicodeLen<0) return std::wstring(L"");
		wchar_t* pUnicode = new wchar_t[unicodeLen + 1];
		if (pUnicode == NULL) return std::wstring(L"");
		memset(pUnicode, 0, sizeof(wchar_t)*(unicodeLen + 1));
		::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
		std::wstring ret = (wchar_t*)pUnicode;
		delete pUnicode;
		return ret;
	}

  

posted @ 2022-07-15 10:17  王牌飞行员_里海  阅读(110)  评论(0编辑  收藏  举报