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.
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With