I would like to transfer an old C++ MFC project from Visual Studio 2005 to a newer version. The project uses a multibyte character set that I know is no longer supported in the current Visual Studio. The first step should therefore be to make the project independent of the character set. A colleague at work told me that I can do this if I put a macro _T() around each text.
Unfortunately, the project contains a lot of static text and adding the macros should take weeks.
Is there no other way?
There is no other way unfortunately. You can try to automate text editing with regex or some text editor like sed.
But personally I would prefer to check all the code manually that no multibyte char-related code is left: use _tcslen instead of strlen, _TCHAR instead of char, etc.
Other variant to consider is to make code explicitly use widechars: wcslen instead of strlen, wchar_t instead of char, L"some string" instead of _T("some string"), etc.
UPD: also I found some good news "The deprecation warning [MFC support for MBCS deprecated] has been removed from MFC in VC2017 and we will continue to provide MBCS support in future releases." (https://blogs.msdn.microsoft.com/vcblog/2013/07/08/mfc-support-for-mbcs-deprecated-in-visual-studio-2013/), so probably you can just left it as it is.
Related
I'm trying to make a xy program which prints ASCII art in the console with chracters such as ⣿, when running the program just prints question marks (?). I understand that its either because of me using the wrong encoding or Microsoft Visual Studio not having the dictionary of these ASCII Characters.
If you have any idea on how to either change encoding or fixing the isue ,it would be much appreciated
Possible solutions:
Try to change the source file encoding to UTF-8 without signature
or UTF-8 with signature.
Try to use wchar_t literal, i.e. std::wcout << L"Your String";.
Learn more:
how to change source file encoding in csharp project (visual studio / msbuild machine)? (Also applies to C++)
What does the 'L' in front a string mean in C++?
There is not a problem with your code but rather a problem with the console that shows your output. It does not show unicode character correctly. In order for it to show these characters correctly it need to recognize unicode and use a font that actually have those characters. To verify this, simple open a cmd window and copy/paste the character into it and see what heppens.
I am trying to display a unicode character (Euro sign) on a button using Qt and C++ in Visual Studio 2013. I tried the following code:
_rotateLeftButton->setText("\u20AC");
and
_rotateLeftButton->setText("€");
and
_rotateLeftButton->setText(QString::fromUtf8("\u20AC"));
and
_rotateLeftButton->setText(QString::fromUtf8("€"));
However, all of those lines result in the following:
All my code files are UTF-8 encoded, except for the moc files (.cxx). For whichever reason the moc executable does not generate them using unicode. Yet I was not able to get this unicode symbol displayed correctly. I also tried setting another font than the default one withouth success. Does anyone know what could be the problem?
Thank you for your help.
QString::fromUtf8("€")
Will work if the file really is handled as UTF-8. As #n.m. commented, VS requires some help from a faux-BOM to ensure this.
QString::fromUtf8("\u20AC")
\u doesn't make sense in a byte string literal. You could spell it using \x byte escapes for the UTF-8 encoded version:
QString::fromUtf8("\xE2\x82\xAC")
Or use a wide string literal:
QString::fromWCharArray(L"\u20AC")
In one of my application I'm developing in c++, I have to display "✓" mark. For that I need to first insert the same in a std::string or in a char. But when I do that, I'm getting a "?" mark as output. I'm using VS2010 to code. Please suggest how to solve the same. Thanks in advance.
There seems to be some basic misunderstanding.
The checkmark character is Unicode 0x2713. You cannot store it as a single character in std::string. The maximum value for a char is 0xff (255). It won't fit.
If you are developing a GUI using C++ for Windows then I would guess MFC. However if you are using std::string then perhaps that's not so. Some choices:
For MFC, you can rebuild your application in UNICODE mode. Then chars are short (16 bits) and your checkmark will fit fine.
You could use std::wstring instead of string. That means changes to existing code.
You could use UTF-8, which replaces the character by a multi-byte sequence. Not recommended in Windows, even if you think you know what you're doing. Very unfriendly.
In any case, if you are using GUI and dialog boxes you will have to make sure they are Unicode dialog boxes or nothing will work.
With a few more details, we could give more specific advice.
you can insert check mark in your console using C++ using the following code
cout << " (\xfb) "<<endl;
Output:
(√)
I want to directly embed non-ASCII Unicode characters in string literals and use them in printf. This implies my source codes must be saved in utf-8 or utf-16. Visual Studio 2010 does support editing and saving C++ source files in either format. But when compiled & executed, it does not produce the correct unicode characters. Does the compiler support string literals with unicode characters embedded?
e.g.
wprintf(L" chinese characters:中文字\n"); the trailing chinese characters cannot be displayed
I don't have a Chinese version of Windows to test with, so this is complete speculation.
The console and file output functions are aware that files are not coded in UTF-16, so they attempt to convert the characters to a code page before output. Just as the default locale is "C" rather than anything based on your system settings, so too the default code page is probably an inappropriate one that does not include Chinese characters.
There is a function SetConsoleOutputCP to change the code page for the console. It is not clear if this function changes the code page used by the actual console window, or if it only affects conversions from Unicode within the program.
The easy way to test wide literals is to skip the formatting part of printf, and give your string straight to the OS: WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L" chinese characters:中文字", ....
It's possible that #pragma setlocale may be what you need.
So I was just going through the basic Windows Programming guide over at MSDN and attempted to do the D2D1Circle Sample in Module 3. The problem I encountered was an error my VC++ 2008 was throwing.
" 'CreateWindowExA' : cannot convert parameter 2 from 'PCWSTR' to 'LPCSTR'"
So, figuring that I had made a slight error while typing the code in I downloaded the sample code rar and opened it up and it threw the exact same error. Any ideas on how I can fix this so it will work. Also, does the fact that I'm programming on a x64 bit machine have anything to do with why it won't work? I know pointers carry different sized values dependent on the machine and both the parameters being called are pointers.
Update # Jollymorphic: In the first few modules, the MSDN tutorial was saying that there really isn't any reason to continue using ascii since unicode covers ascii and also supports all other languages like Chinese, Japanese, etc. Wouldn't implementing your solution cause my program to only support ascii and subsequently not allow support for east asian languages?
A PCWSTR is a pointer to wide (16-bit) characters. An LPCSTR is a pointer to regular (8-bit) characters. Your project probably is set to generate code based on the UNICODE character set. If you open the properties for your project in Visual Studio, and then navigate to the "General" page, you'll see a "Character Set" property. If it is currently set to "Use Unicode character set," then you can change it to "Use Multi-Byte character set," and your string literals will be generated as 8-bit character strings.