Writing

Solving Visual Studio Problems with non-UTF-8 Encoding

This post relates to Visual Studio not Visual Studio Code .

Default Encoding

Problem:

In different regions, Visual Studio’s default encoding may be set to the local regional encoding, not UTF-8.

For example:

Microsoft Visual C++ (MSVC) on Korean Windows defaults to reading and writing source files as CP949.

This means that files containing non-English characters (한글), when saved in the regional encoding, and opened or viewed in UTF-8, will display broken symbols.

To display the contents correctly, the file must be reopened or viewed in the original regional encoding (EUC-KR).

Save with UTF-8 Encoding

Solution:

Save files to UTF-8 Encoding

There are two methods:

1. “Save As…”

  1. Select “File -> Save As…”
  2. In the Save As window, click the dropdown arrow beside “Save”
  3. Select “Save with encoding…”
  4. Select “UTF-8 Codepage 65001”

Problem:

  • This method requires selecting “Save As…” at least once for EVERY file in the project. (Not viable for large projects.)

2. Add an .editorconfig file

  1. In the project’s root directory, create an .editorconfig file
  2. Define consistent coding styles (such as character encoding, line endings, etc) in the file (example below)

Problems:

  1. The .editorconfig file’s settings only apply to NEW files. Old files will still need to be “Saved As…”
  2. If the charset line does not contain bom then MSVC will still try to open files in the default regional encoding (because only utf-8-bom forces both saving and loading files into UTF-8). This will prevent program execution and produce dozens of “unknown character” errors in otherwise perfect code.

Fix charset = utf-8 code execution errors

If using .editorconfig to save files to UTF-8, but the charset line does NOT contain bom, the command line interpret will break.

Again, the .editorconfig file’s settings only apply to NEW files, so adding bom after the fact will still require “Save As…”.

Solution:

Add /utf-8 as an interpreter option in the Terminal

  1. In the “Project” menu -> “Test Properties”
  2. Under “Configuration Properties” -> “C/C++” -> “Command Line (Terminal)”
  3. In “Additional Options” add /utf-8

Let Visual Studio manage EditorConfig

Or, alternatively, Visual Studio can manage your .editorconfig file for you. The following page shows many options and methods for working with EditorConfig directly within Visual Studio itself.

Visual Studio IDE: Define consistent coding styles with EditorConfig

Discussion댓글