Setting Console Encoding to Unicode

When you try to set console (input or output) encoding to Unicode or UTF32

Console.OutputEncoding = Encoding.Unicode;

you receive an error message

The parameter is incorrect

To set the console’s encoding, you need to retrieve stdin or stdout, create a new StreamReader with the Encoding, and assign it to the console using SetIn() or SetOut():

var s = Console.OpenStandardInput();
Console.SetIn(new StreamReader(s, inenc));

StreamWriter requires the AutoFlush property set to true:

var s = Console.OpenStandardOutput();
Console.SetOut(new StreamWriter(s, outenc) { AutoFlush = true });

One Response to Setting Console Encoding to Unicode

  1. […] utilities provide command-line switches to select the encoding of the input and output streams (-in for stdin or Console.In, and -out for stdout or Console.Out), […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: