When I open cmd.exe in Windows, what encoding is it using?
How can I check which encoding it is currently using? Does it depend on my regional setting or are there any environment variables to check?
What happens when you type a file with a certain encoding? Sometimes I get garbled characters (incorrect encoding used) and sometimes it kind of works. However I don't trust anything as long as I don't know what's going on. Can anyone explain?
To answer your second query re. how encoding works, Joel Spolsky wrote a great introductory article on this. Strongly recommended.
I've been frustrated for long by Windows code page issues, and the C programs portability and localisation issues they cause. The previous posts have detailed the issues at length, so I'm not going to add anything in this respect.
To make a long story short, eventually I ended up writing my own UTF-8 compatibility library layer over the Visual C++ standard C library. Basically this library ensures that a standard C program works right, in any code page, using UTF-8 internally.
This library, called MsvcLibX, is available as open source at https://github.com/JFLarvoire/SysToolsLib. Main features:
More details in the MsvcLibX README on GitHub, including how to build the library and use it in your own programs.
The release section in the above GitHub repository provides several programs using this MsvcLibX library, that will show its capabilities. Ex: Try my which.exe tool with directories with non-ASCII names in the PATH, searching for programs with non-ASCII names, and changing code pages.
Another useful tool there is the conv.exe program. This program can easily convert a data stream from any code page to any other. Its default is input in the Windows code page, and output in the current console code page. This allows to correctly view data generated by Windows GUI apps (ex: Notepad) in a command console, with a simple command like:
type WINFILE.txt | conv
This MsvcLibX library is by no means complete, and contributions for improving it are welcome!
Command CHCP shows the current codepage. It has three digits: 8xx and is different from Windows 12xx. So typing a English-only text you wouldn't see any difference, but an extended codepage (like Cyrillic) will be printed wrongly.
Type
to see your current code page (as Dewfy already said).
Use
to see all installed code pages and find out what your code page number means.
You need to have Windows Server 2003 Resource kit installed (works on Windows XP) to use
nlsinfo
.Yes, it’s frustrating—sometimes
type
and other programs print gibberish, and sometimes they do not.First of all, Unicode characters will only display if the current console font contains the characters. So use a TrueType font like Lucida Console instead of the default Raster Font.
But if the console font doesn’t contain the character you’re trying to display, you’ll see question marks instead of gibberish. When you get gibberish, there’s more going on than just font settings.
When programs use standard C-library I/O functions like
printf
, the program’s output encoding must match the console’s output encoding, or you will get gibberish.chcp
shows and sets the current codepage. All output using standard C-library I/O functions is treated as if it is in the codepage displayed bychcp
.Matching the program’s output encoding with the console’s output encoding can be accomplished in two different ways:
A program can get the console’s current codepage using
chcp
orGetConsoleOutputCP
, and configure itself to output in that encoding, orYou or a program can set the console’s current codepage using
chcp
orSetConsoleOutputCP
to match the default output encoding of the program.However, programs that use Win32 APIs can write UTF-16LE strings directly to the console with
WriteConsoleW
. This is the only way to get correct output without setting codepages. And even when using that function, if a string is not in the UTF-16LE encoding to begin with, a Win32 program must pass the correct codepage toMultiByteToWideChar
. Also,WriteConsoleW
will not work if the program’s output is redirected; more fiddling is needed in that case.type
works some of the time because it checks the start of each file for a UTF-16LE Byte Order Mark (BOM), i.e. the bytes0xFF 0xFE
. If it finds such a mark, it displays the Unicode characters in the file usingWriteConsoleW
regardless of the current codepage. But whentype
ing any file without a UTF-16LE BOM, or for using non-ASCII characters with any command that doesn’t callWriteConsoleW
—you will need to set the console codepage and program output encoding to match each other.How can we find this out?
Here’s a test file containing Unicode characters:
Here’s a Java program to print out the test file in a bunch of different Unicode encodings. It could be in any programming language; it only prints ASCII characters or encoded bytes to
stdout
.The output in the default codepage? Total garbage!
However, what if we
type
the files that got saved? They contain the exact same bytes that were printed to the console.The only thing that works is UTF-16LE file, with a BOM, printed to the console via
type
.If we use anything other than
type
to print the file, we get garbage:From the fact that
copy CON
does not display Unicode correctly, we can conclude that thetype
command has logic to detect a UTF-16LE BOM at the start of the file, and use special Windows APIs to print it.We can see this by opening
cmd.exe
in a debugger when it goes totype
out a file:After
type
opens a file, it checks for a BOM of0xFEFF
—i.e., the bytes0xFF 0xFE
in little-endian—and if there is such a BOM,type
sets an internalfOutputUnicode
flag. This flag is checked later to decide whether to callWriteConsoleW
.But that’s the only way to get
type
to output Unicode, and only for files that have BOMs and are in UTF-16LE. For all other files, and for programs that don’t have special code to handle console output, your files will be interpreted according to the current codepage, and will likely show up as gibberish.You can emulate how
type
outputs Unicode to the console in your own programs like so:This program works for printing Unicode on the Windows console using the default codepage.
For the sample Java program, we can get a little bit of correct output by setting the codepage manually, though the output gets messed up in weird ways:
However, a C program that sets a Unicode UTF-8 codepage:
does have correct output:
The moral of the story?
type
can print UTF-16LE files with a BOM regardless of your current codepageWriteConsoleW
.chcp
, and will probably still get weird output.