title: C# encoding conversion to UTF-8 - different behavior on Windows 7 vs. Windows Server 2008 R2?
body: <p>I am encountering some strange behavior between Windows platforms when it comes to encoding conversion to UTF-8.  If I have a string with "Extended ASCII" characters in it, say like character 0x99 for the trademark symbol, I can convert it on Windows 7 with the following code:</p>
  
  <pre><code>using System.Text;
  ...
  string DefaultEncodedStr = &lt;&lt;Some string with Extended ASCII chars like 0x99 for TM&gt;&gt;
  byte[] DefaultEncodedBytes = Encoding.Default.GetBytes(DefaultEncodedStr);
  byte[] UTF8EncodedBytes = Encoding.Convert(Encoding.Default, Encoding.UTF8, DefaultEncodedBytes);
  char[] UTF8Chars = new char[Encoding.UTF8.GetCharCount(UTF8EncodedBytes, 0, UTF8EncodedBytes.Length)];
  UTF8.GetChars(UTF8EncodedBytes, 0, UTF8EncodedBytes.Length, UTF8Chars, 0);
  string UTF8Str = new string(UTF8Chars); 
  </code></pre>
  
  <p>However, this code fails to convert the initial string on Windows Server 2008 R2 -- the "Extended ASCII" characters are still present in the final string.  </p>
  
  <p>This code, however, does work on Windows Server 2008 R2:</p>
  
  <pre><code>using System.Text;
  ...
  string DefaultEncodedStr = &lt;&lt;Some string with Extended ASCII chars like 0x99 for TM&gt;&gt;
  byte[] DefaultEncodedBytes = Encoding.Default.GetBytes(DefaultEncodedStr)
  string UTF8Str = Encoding.UTF8.GetString(DefaultEncodedBytes); 
  </code></pre>
  
  <p>BUT, this code fails to convert the string on Windows 7!!</p>
  
  <p>What gives?  Why am I seeing different behavior between the Desktop class and the Server class OS that is using the same kernel base (Win7)?  Do I really need to include some kind of platform detection code to determine how to properly handle the encoding conversion??</p>