デフォルトで、Visual Studio にすべての文字列に UTF-8 エンコーディングを使用させることはできますか?
たとえば、
wchar_t *txt="hello";
utf8でエンコード
デフォルトで、Visual Studio にすべての文字列に UTF-8 エンコーディングを使用させることはできますか?
たとえば、
wchar_t *txt="hello";
utf8でエンコード
This blog article looks promising: UTF-8 strings and Visual C++
Most of the important content is still there, even though some of the pictures are broken. In short:
First step, you have to make sure the source file is UTF-8 encoded with the byte order mark (BOM). The BOM is an extremely important thing, without it the C++ compiler will not behave correctly.
In Visual Studio 2008, this can be done directly from the IDE with the Advanced save command located in the File menu. A dialog box will pop up. Select UTF-8 with signature.
If you compile and run a test program, [you are not going to get the expected result.] What happens is that, although your text is properly encoded in UTF-8, for compatibility reasons the C/C++ runtime is by default set to the “C” locale. This locale assumes that all char are 1 byte. Erm. Not quite the case with UTF-8 my dear!
You need to change the locale with the
setlocale
function to have the string properly interpreted by the input output stream processors.In our case, the locale of whatever the system is using is fine, this is done in passing “” as the second parameter.
To be rigorous, you must check the return value of setlocale, if it returns 0, an error occurred. In multi-language applications, you will need to use setlocale with more precision, explicitly supplying the locale you want to use (for example you may want to have your application display Russian text on a Japanese computer).
I don't know of any good way to make this the default. I'm pretty sure it's not possible. Windows applications strongly prefer UTF-16, if you're compiling for Unicode. If at all possible, you should convert to that format.
Otherwise, the best possible option I can come up with is to define a simple macro (something akin to _T("string")
defined in the Windows headers) that converts to UTF-8 using the above logic.