Before you get started; Yes, I know this is a duplicate question, and yes, I looked at the published solutions. My problem is that I could not get them to work.
bool invalidChar (char c)
{
return !isprint((unsigned)c);
}
void stripUnicode(string & str)
{
str.erase(remove_if(str.begin(),str.end(), invalidChar), str.end());
}
I tested this method on "Prusæus, Ægyptians" and it didn’t do anything. I also tried to replace it isprintwithisalnum
The real problem arises when in another section of my program I convert the string string-> wstring->. conversion prohibits if there are Unicode characters in the string-> wstring conversion.
Ref:
How can you strip non-ASCII characters from a string? (in c #)
How to remove all non-alphanumeric characters from a string in C ++?
Edit:
I would still like to remove all non-ASCII characters, no matter if this helps, this is where I crashed:
wchar_t* UnicodeTextBuffer = new wchar_t[ANSIWord.length()+1];
wmemset(UnicodeTextBuffer, 0, ANSIWord.length()+1);
mbstowcs(UnicodeTextBuffer, ANSIWord.c_str(), ANSIWord.length());
wWord = UnicodeTextBuffer;
Error dialog
MSV++
!
://myproject
: f:\dd\vctools\crt_bld\self_x86\crt\src\isctype.c
://
( ) ( + 1) <= 256
Edit:
: .txt, , ANSI. .
:
bool invalidChar (char c)
{
return !(c>=0 && c <128);
}
void stripUnicode(string & str)
{
str.erase(remove_if(str.begin(),str.end(), invalidChar), str.end());
}
- / , .
EDIT:
: __ isascii, iswascii