原理:寬字節字符串與多字節字符串之間轉換無法轉換的字符默認會轉換成'?'。
多字節每個字符的字節數不是固定的,而寬字節的每個字符固定的,所以要轉換回寬字節字符串判斷那個字符是無法轉換的字符。
CString FindUnknownChar(LPCTSTR lpString)
{CString str(lpString);
CStringA strA;
int iSize = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, str, -1, strA.GetBuffer(iSize), iSize+1, NULL, NULL);
strA.ReleaseBuffer();
CString strW;
iSize = MultiByteToWideChar(CP_ACP, 0, strA, -1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, strA, -1, strW.GetBuffer(iSize), iSize);
strW.ReleaseBuffer();
CString strFind;
int i=0;
while(i!=-1)
{
i = strW.Find(_T('?'), i);
if (i != -1)
{
strFind += str.GetAt(i);
i ++;
}
}
return strFind;
}