Stripping null bytes from a unicode string
<p>My paths are UTF-16 strings. Most of them only use the ASCII set, so a filename like <code>test</code> would be stored as</p>

<pre><code>T \x00 E \x00 S \x00 T \x00
</code></pre>

<p>I use <code>Encoding.Unicode.GetString(bytes)</code> to read the string and it works fine (when I print them out to console or to a form control it appears as I expect), but when I want to actually create a file with the given filename using the following code</p>

<pre><code>BinaryWriter outFile = new BinaryWriter(File.OpenWrite(path));
</code></pre>

<p>I get an exception</p>

<pre><code>Unhandled Exception: System.ArgumentException: Illegal characters in path.
   at System.IO.Path.CheckInvalidPathChars(String path)
   at System.IO.Path.GetFileName(String path)
</code></pre>

<p>It's probably because there are null chars in there (maybe it stores the original byte array internally), but I'm not sure how to deal with it. Not all of the strings are ASCII though, and some of the characters use two-bytes.</p>

<p>UPDATE:</p>

<p>Turns out the illegal bytes were simply null bytes that were padded to the string. However, I can't simply strip all trailing null bytes, but I don't know the length of the string either. How would I strip null bytes from a string where each character is stored in n bytes?</p>
