<p>I'm trying to write a wstring to file with ofstream in binary mode, but I think I'm doing something wrong. This is what I've tried:</p>

<pre><code>ofstream outFile("test.txt", std::ios::out | std::ios::binary);
wstring hello = L"hello";
outFile.write((char *) hello.c_str(), hello.length() * sizeof(wchar_t));
outFile.close();
</code></pre>

<p>Opening test.txt in for example Firefox with encoding set to UTF16 it will show as:</p>

<p>h�e�l�l�o�</p>

<p>Could anyone tell me why this happens?  </p>

<p><strong>EDIT:</strong></p>

<p>Opening the file in a hex editor I get:</p>

<pre><code>FF FE 68 00 00 00 65 00 00 00 6C 00 00 00 6C 00 00 00 6F 00 00 00 
</code></pre>

<p>Looks like I get two extra bytes in between every character for some reason?</p>

You should look at the output file in a hex editor such as
WinHex
so you can see the actual bits and bytes, to verify that the output is actually UTF-16.  Post it here and let us know the result.  That will tell us whether to blame Firefox or your C++ program.
But it looks to me like your C++ program works and Firefox is not interpreting your UTF-16 correctly.  UTF-16 calls for two bytes for every character.  But Firefox is printing twice as many characters as it should, so it is probably trying to interpret your string as UTF-8 or ASCII, which generally just have 1 byte per character.
When you say "Firefox with encoding set to UTF16" what do you mean?  I'm skeptical that that work work.