Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"The parameter is incorrect" when setting Unicode as console encoding

Tags:

c#

encoding

I get the following error:

Unhandled Exception: System.IO.IOException: The parameter is incorrect.
 at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
 at System.IO.__Error.WinIOError()
 at System.Console.set_OutputEncoding(Encoding value)
 at (my program)

when I run the following line of code:

 Console.OutputEncoding = Encoding.Unicode;

Any idea why? I do not get this error if I set the encoding to UTF8 instead.

like image 651
Epaga Avatar asked Jan 07 '09 07:01

Epaga


2 Answers

Encoding.Unicode is UTF-16 which uses 2 bytes to encode all characters. The ASCII characters (English characters) are the same in UTF-8 (single bytes, same values), so that might be why it works.

My guess is that the Windows Command Shell doesn't fully support Unicode. Funny that the Powershell 2 GUI does support UTF-16 (as far as I know), but the program throws the same exception there.

The following code works which shows that the Console object can have its output redirected and support Encoding.Unicode:

FileStream testStream = File.Create("test.txt");
TextWriter writer = new StreamWriter(testStream, Encoding.Unicode);
Console.SetOut(writer);            
Console.WriteLine("Hello World: \u263B");  // unicode smiley face
writer.Close(); // flush the output
like image 137
user10789 Avatar answered Oct 05 '22 07:10

user10789


According to the list of Code Page Identifiers on MSDN, the UTF-16 and UTF-32 encodings are managed-only:

1200   utf-16       Unicode UTF-16, little endian byte order (BMP of ISO 10646); available only to managed applications
1201   unicodeFFFE  Unicode UTF-16, big endian byte order; available only to managed applications
12000  utf-32       Unicode UTF-32, little endian byte order; available only to managed applications
12001  utf-32BE     Unicode UTF-32, big endian byte order; available only to managed applications

For instance, they're not listed in the registry with the other system code pages under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage.

like image 41
Trevor Robinson Avatar answered Oct 05 '22 07:10

Trevor Robinson