Alternate method Encoding.Unicode.GetBytes in native C++
<p>Need implement <code>Encoding.Unicode.GetBytes</code> in native <code>C++</code>.</p>

<p><code>.NET</code> implementation:</p>

<pre><code>Console.WriteLine("codePage number: " + Encoding.Unicode.CodePage.ToString());
Console.Write("string: ");
foreach (var ch in Encoding.Unicode.GetBytes("string"))
    Console.Write(ch.ToString("X") + "-");
Console.WriteLine();
Console.Write("строка: ");
foreach (var ch in Encoding.Unicode.GetBytes("строка"))
    Console.Write(ch.ToString("X") + "-");
Console.ReadLine();
</code></pre>

<p><code>.NET</code> implementation output: </p>

<pre><code>codePage number: 1200 
string: 73-0-74-0-72-0-69-0-6E-0-67-0 
строка: 41-4-42-4-40-4-3E-4-3A-4-30-4
</code></pre>

<p>How implement this method (without use boost, QT, etc..) to C++?</p>

<hr>

<p>I found this method from <code>Windows</code>:</p>

<pre><code>#include &lt;exception&gt;
#include &lt;iostream&gt;
#include &lt;ostream&gt;
#include &lt;string&gt;
#include &lt;Windows.h&gt;

std::wstring ConvertToUTF16(const std::string &amp; source, const UINT codePage)
{
    // Fail if an invalid input character is encountered
    static const DWORD conversionFlags = MB_ERR_INVALID_CHARS;

    // Require size for destination string
    int utf16Length = ::MultiByteToWideChar(
        codePage,           // code page for the conversion
        conversionFlags,    // flags
        source.c_str(),     // source string
        source.length(),    // length (in chars) of source string
        NULL,               // unused - no conversion done in this step
        0                   // request size of destination buffer, in wchar_t's
    );
    if (utf16Length == 0)
    {
        const DWORD error = ::GetLastError();
        throw std::exception(
            "MultiByteToWideChar() failed: Can't get length of destination UTF-16 string.",
            error);
    }

    // Allocate room for destination string
    std::wstring utf16Text;
    utf16Text.resize(utf16Length);

    // Convert to Unicode
    if (!::MultiByteToWideChar(
        codePage,           // code page for conversion
        0,                  // validation was done in previous call
        source.c_str(),     // source string
        source.length(),    // length (in chars) of source string
        &amp;utf16Text[0],      // destination buffer
        utf16Text.length()  // size of destination buffer, in wchar_t's
    ))
    {
        const DWORD error = ::GetLastError();
        throw std::exception(
            "MultiByteToWideChar() failed: Can't convert to UTF-16 string.",
            error);
    }

    return utf16Text;
}

void main()
{
    try
    {
        // ASCII text
        std::string inText("string");

        // Unicode
        static const UINT codePage = 1200;

        // Convert to Unicode
        const std::wstring utf16Text = ConvertToUTF16(inText, codePage);

        // Show result
        for (size_t i = 0; i &lt; utf16Text.size(); i++)
            printf("%X-", utf16Text[i]);
    }
    catch (const std::exception&amp; e)
    {
        std::cerr &lt;&lt; "*** ERROR:\n";
        std::cerr &lt;&lt; e.what();
        std::cerr &lt;&lt; std::endl;
    }

    getchar();
}
</code></pre>

<p>but <code>MultiByteToWideChar</code> no return string size for 1200 code page (<code>Unicode</code>).</p>