#include "CharConvert.h" #pragma region 字符串编码处理 #pragma region 字符转换 char* CharConvert::StringTocharPtrA(string* sourceStr) { char* ch = (LPSTR)(LPCTSTR)sourceStr; return ch; } wchar_t* CharConvert::wstringToWchar_tPtrW(wstring ws) { wchar_t* ptr = _wcsdup(ws.c_str()); return ptr; } char* CharConvert::GetWstringToCharPtr(wstring ws) { wchar_t* ptr = _wcsdup(ws.c_str()); char* ch = CharConvert::GetMultiByteW(ptr); return ch; } #pragma endregion #pragma region Unicode和ANSI wstring CharConvert::GetWideCharW(char* multiByte) { char* ch = multiByte; int len = MultiByteToWideChar(CP_ACP, 0, ch, -1, NULL, 0); wchar_t* wch = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, ch, -1, wch, len); return wch; } wstring CharConvert::GetWideCharW(string* multiByte) { //(LPSTR)(LPCTSTR)会乱码,之后会在处理所有转换字符的问题 const char* ch2 = /*(LPSTR)(LPCTSTR)*/multiByte->data(); char* ch = (char*)ch2; wstring wch = GetWideCharW(ch); return wch; } char* CharConvert::GetMultiByteW(wchar_t* wideChar) { char* multiByte; int iwstrLen = WideCharToMultiByte(CP_ACP, 0, wideChar, -1, 0, 0, 0, 0); multiByte = new char[iwstrLen + 1]; memset(multiByte, 0, sizeof(char) * (iwstrLen + 1)); WideCharToMultiByte(CP_ACP, 0, wideChar, -1, multiByte, iwstrLen, 0, 0); return multiByte; } char* CharConvert::GetMultiByteUTF8W(wchar_t* wideChar) { char* multiByte; int iwstrLen = WideCharToMultiByte(CP_UTF8, 0, wideChar, -1, 0, 0, 0, 0); multiByte = new char[iwstrLen + 1]; memset(multiByte, 0, sizeof(char) * (iwstrLen + 1)); WideCharToMultiByte(CP_UTF8, 0, wideChar, -1, multiByte, iwstrLen, 0, 0); return multiByte; } char* CharConvert::GetMultiByteW(wstring wideStr) { wchar_t* ptr = _wcsdup(wideStr.c_str()); char* multiByte = GetMultiByteW(ptr); return multiByte; } std::wstring CharConvert::ReplaceSubStrW(wstring sourceStr, const wstring& oldSubStr, const wstring& newSubStr) { wstring strRet = sourceStr; size_t pos = 0; int l_count = 0; int count = -1; if (-1 == count) // replace all count = strRet.size(); while ((pos = strRet.find(oldSubStr, pos)) != wstring::npos) { strRet.replace(pos, oldSubStr.size(), newSubStr); if (++l_count >= count) break; pos += newSubStr.size(); } return strRet; } #pragma endregion #pragma endregion