Reputation: 167
I have string input. I want to check all the characters and prompt the user if there is any Unicode character in input string.
How can I do this validation in C++.
eg. In Notepad if you enter any Unicode character and try to save it with ANSI Encoding, it will prompt about Unicode character. I want to do similar validation.
Upvotes: 3
Views: 8119
Reputation: 936
You can use IsTextUnicode function. That's the function notepad uses as far as I know.
MSDN-Link: http://msdn.microsoft.com/en-us/library/windows/desktop/dd318672%28v=vs.85%29.aspx
Just insert NULL as the last parameter.
#include <string>
#include <Windows.h>
int main()
{
std::string s = "Hallo!";
std::wstring ws = L"Hello!";
if (::IsTextUnicode(ws.c_str(), ws.length(), NULL) == 1)
{
// is unicode
int i = 0;
}
else
{
// no unicode
int i = 1;
}
return 0;
}
Upvotes: 3
Reputation: 180235
What Notepad warns you about is slightly different: It warns you about Unicode characters that cannot be converted to the desired code page. IOW, WideCharToMultiByte(CP_ACP, ..., &lpUsedDefaultChar)
causes lpUsedDefaultChar
to be set to TRUE.
Substitute CP_ACP
for the encoding you want, except CP_UTF8
which makes no sense. UTF8 supports all Unicode characters.
Upvotes: 1
Reputation: 409442
An easy way is to allow Unicode and store the text as UTF-8. As UTF-8 is a superset of ASCII it's very easy to find characters which are not ASCII (they have the high bit set).
Upvotes: 1