여기 내 상황이 있습니다. 사용자가 단어에서 붙여 넣거나 문서를 Excel에서 제외 할 수있는 잘못된 문자를 필터링해야하는 문제가 있습니다.C++ - 사용자가 그리드에 붙여 넣을 때 잘못된 문자 제거
내가하는 일은 다음과 같습니다.
우선 나는 지금이 31
String __fastcall TMainForm::filterInput(String l_sConversion)
{
// Used to store every character that was stripped out.
String filterChars = "";
// Not Used. We never received the whitelist
String l_SWhiteList = "";
// Our String without the invalid characters.
AnsiString l_stempString;
// convert the string into an array of chars
wchar_t* outputChars = l_sConversion.w_str();
char * pszOutputString = NULL;
//convert any unicode characters to ASCII
ConvertUnicodeToAscii(outputChars, pszOutputString);
l_stempString = (AnsiString)pszOutputString;
//We're going backwards since we are removing characters which changes the length and position.
for (int i = l_stempString.Length(); i > 0; i--)
{
char l_sCurrentChar = l_stempString[i];
//If we don't have a valid character, filter it out of the string.
if (((unsigned int)l_sCurrentChar < 31) ||((unsigned int)l_sCurrentChar > 127))
{
String l_sSecondHalf = "";
String l_sFirstHalf = "";
l_sSecondHalf = l_stempString.SubString(i + 1, l_stempString.Length() - i);
l_sFirstHalf = l_stempString.SubString(0, i - 1);
l_stempString = l_sFirstHalf + l_sSecondHalf;
filterChars += "\'" + ((String)(unsigned int)(l_sCurrentChar)) + "\' ";
}
}
if (filterChars.Length() > 0)
{
LogInformation(__LINE__, __FUNC__, Utilities::LOG_CATEGORY_GENERAL, "The Following ASCII Values were filtered from the string: " + filterChars);
}
// Delete the char* to avoid memory leaks.
delete [] pszOutputString;
return l_stempString;
}
127 사이의 값이없는 모든 문자를 필터링하고있어
extern "C" COMMON_STRING_FUNCTIONS long ConvertUnicodeToAscii(wchar_t * pwcUnicodeString, char* &pszAsciiString)
{
int nBufLen = WideCharToMultiByte(CP_ACP, 0, pwcUnicodeString, -1, NULL, 0, NULL, NULL)+1;
pszAsciiString = new char[nBufLen];
WideCharToMultiByte(CP_ACP, 0, pwcUnicodeString, -1, pszAsciiString, nBufLen, NULL, NULL);
return nBufLen;
}
다음을 ASCII로 유니 코드 문자를 변환하려고 해요 단어 문서에서 글 머리 기호를 복사하려고하면 제외하고 작동하는 것 같습니다.
o Bullet1 :
subbullet1.
이
oBullet1? subbullet1 같은 것을 얻을 것이다.
내 필터 기능은 onchange 이벤트에서 호출됩니다.
글 머리 기호는 값 o와 물음표로 대체됩니다.
내가 뭘 잘못하고 있고, 이것을 시도하는 더 좋은 방법이있다.
저는 Visual C++ 솔루션을 사용하지 않으므로 C++ 빌더 XE5를 사용하고 있습니다. 당신이 (실제로 BTW, ASCII로 변환하지입니다) ASCII로 변환을 수행 할 때
는'CP_ACP'는 ASCII를 나타내지 않는, 그것은 어떤 언어가 될 수있는 OS의 현재 로케일을 나타냅니다
이와 비슷한 더 많은 것을보십시오. ASCII 자체는 대신 코드 페이지 20127입니다. 또한'AnsiStringT <20127>'을 대신 사용할 수 있고 RTL이 변환을 처리 할 수있을 때 자신의 변환 함수를 정의하는 것은 불필요합니다. –