VS 字串 ASSIC Unicode UTF-8轉換
CString Utf8ToLocal(const char* utf8Str)
{
CString strLocal = _T("");
if (utf8Str == NULL)
{
return strLocal;
}
int lengthOfWcs = MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, NULL, 0);
wchar_t *wcs = new wchar_t[lengthOfWcs];
MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, wcs, lengthOfWcs);
int lengthOfMbs = WideCharToMultiByte(CP_ACP, 0, wcs, lengthOfWcs, NULL, 0, 0, 0);
char * localStr = new char[lengthOfMbs];
WideCharToMultiByte(CP_ACP, 0, wcs, lengthOfWcs, localStr, lengthOfMbs, 0, 0);
strLocal.Format("%s", localStr);
if (wcs != NULL)
{
delete wcs;
wcs = NULL;
}
if (localStr != NULL)
{
delete localStr;
localStr = NULL;
}
return strLocal;
}
CString LocalToUtf8(const char *localStr)
{
CString strUtf8 = _T("");
if (localStr == NULL)
{
return strUtf8;
}
int lengthOfWcs = MultiByteToWideChar(CP_ACP, 0, localStr, -1, NULL, 0);
wchar_t *wcs = new wchar_t[lengthOfWcs];
MultiByteToWideChar(CP_ACP, 0, localStr, -1, wcs, lengthOfWcs);
int lengthOfMbs = WideCharToMultiByte(CP_UTF8, 0, wcs, lengthOfWcs, NULL, 0, 0, 0);
char* utf8Str = new char[lengthOfMbs];
WideCharToMultiByte(CP_UTF8, 0, wcs,lengthOfWcs, utf8Str, lengthOfMbs, 0, 0);
strUtf8.Format("%s", utf8Str);
if (wcs != NULL)
{
delete wcs;
wcs = NULL;
}
if (utf8Str != NULL)
{
delete utf8Str;
utf8Str = NULL;
}
return strUtf8;
}
CString WideCharToANSIChar(const CStringW& wideString)
{
CString ansiString = "";
LPSTR tmpStr = NULL;
int charNeeded = WideCharToMultiByte(CP_ACP, 0, wideString, -1, 0, NULL, NULL, 0);
tmpStr = new char[charNeeded + 1];
if (NULL == tmpStr)
{
return ansiString;
}
memset(tmpStr, 0, (charNeeded + 1)*sizeof(char));
WideCharToMultiByte(CP_ACP, NULL, wideString, -1, tmpStr, charNeeded, NULL, 0);
if (0 == charNeeded)
{
tmpStr = NULL;
}
else
{
tmpStr[charNeeded] = '\0';
}
ansiString = tmpStr;
delete[] tmpStr;
tmpStr = NULL;
return ansiString;
}
CStringW ANSICharToWideChar(const CString& ansiString)
{
CStringW wideString = L"";
LPWSTR tmpStr = NULL;
int charNeeded = MultiByteToWideChar(CP_ACP, 0, ansiString, -1, NULL, 0);
tmpStr = new WCHAR[charNeeded + 1];
if (NULL == tmpStr)
{
return wideString;
}
memset(tmpStr, 0, (charNeeded + 1)*sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, ansiString, -1, tmpStr, charNeeded);
if (0 == charNeeded)
{
tmpStr = NULL;
}
else
{
tmpStr[charNeeded] = '\0';
}
wideString = tmpStr;
delete[] tmpStr;
tmpStr = NULL;
return wideString;
}
int SpliteStringByTag(const CString &strTarget, STRING_VECTOR &vStr, const CString &strTag)
{
vStr.clear();
int iCount = 0;
CString strTmp = strTarget;
strTmp.TrimLeft();
strTmp.TrimRight();
int nPos = strTmp.Find(strTag);
if (nPos >= 0)
{
while (nPos >= 0)
{
CString strSubStr = strTmp.Left(nPos);
if (!strSubStr.IsEmpty())
{
vStr.push_back(strSubStr);
iCount++;
}
strTmp = strTmp.Right(strTmp.GetLength() - nPos - strTag.GetLength());
nPos = strTmp.Find(strTag);
}
if (!strTmp.IsEmpty())
{
vStr.push_back(strTmp);
iCount++;
}
}
else if (strTmp.GetLength() > 0)
{
vStr.push_back(strTmp);
iCount++;
}
return iCount;
}
CString GetSystemVariable(const CString &strSysVar)
{
CString strReturn = _T("");
int nLength;
STRING_VECTOR vStr;
nLength = strSysVar.GetLength();
if (nLength > 0)
{
strReturn = strSysVar;
strReturn.Replace("/","\\");
SpliteStringByTag(strReturn, vStr, "\\");
if (vStr.size() > 0)
{
CString strTmp;
strTmp = vStr[0];
if (strTmp.Find('%') != -1)
{
int nCount;
strTmp.TrimLeft('%');
strTmp.TrimRight('%');
nCount = GetEnvironmentVariable(strTmp, NULL, 0);
if (nCount > 0)
{
char *buffer = new char[nCount+1];
nCount = GetEnvironmentVariable(strTmp, buffer, nCount+1);
strTmp = buffer;
delete[] buffer;
nCount = strReturn.Find('\\');
if (nCount != -1)
{
strReturn = strReturn.Right(nLength - nCount);
strReturn = strTmp + strReturn;
}
else
strReturn = strTmp;
}
}
}
}
return strReturn;
}
CString GetStringMD5(CString str)
{
CMD5 md5;
md5.setPlainText(str.GetBuffer(str.GetLength()));
str.ReleaseBuffer();
return md5.getMD5Digest();
}
void EncryptBuffer(LPTSTR buffer, int len, BOOL isEncrypt, LPTSTR password)
{
CRC4 encrypt;
if (strlen(password) == 0)
{
password = U_STRING_ENCRYPT_PASSWORD;
}
if (isEncrypt)
{
// 加密快取
encrypt.Encrypt(buffer, len, password);
}
else
{
// 解密快取
encrypt.Decrypt(buffer, len, password);
}
}
CString COleDateTime2CString(COleDateTime& inValue)
{
return inValue.Format("%Y-%m-%d %H:%M:%S");
}
COleDateTime CString2COleDateTime(CString inValue)
{
int nYear = 1900;
int nMonth = 1;
int nDate = 1;
int nHour = 0;
int nMin = 0;
int nSec = 0;
sscanf(inValue, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);
return (COleDateTime(nYear, nMonth, nDate, nHour, nMin, nSec));
}