What is alternative for MultibyteToWideChar and WideCharToMultiByte functions in .NET?
<p>I am trying to migrate a code from VC++ to .net. VC++ code uses MultibyteToWideChar and WideCharToMultiByte functions provided by WinAPI. I tried using System.Text.Encoding class in .NET but it is not working for all encodings. Is there any other way to do this conversion? What is wrong in below code snippet?</p>

<p>Here is my C# code:</p>

<pre><code>public static string MultiByteToWideChar(string input, int codepage)
    {
        Encoding e1 = Encoding.GetEncoding(codepage);
        Encoding e2 = Encoding.Unicode;

        //byte[] source = e1.GetBytes(input);

        byte[] source = MBCSToByte(input);

        byte[] target = Encoding.Convert(e1, e2, source);

        return e2.GetString(target);
    }
public static string WideCharToMultiByte(string input, int codepage)
    {
        Encoding e1 = Encoding.Unicode;
        Encoding e2 = Encoding.GetEncoding(codepage);

        byte[] source = e1.GetBytes(input);

        byte[] target = Encoding.Convert(e1, e2, source);

        return Encoding.GetEncoding(codepage).GetString(target);

    }
private static byte[] MBCSToByte(string s)
    {
        byte[] b = new byte[s.Length];
        int i = 0;
        foreach (char c in s)
            b[i++] = (byte)c;
        return b;
    }
</code></pre>

<p>MultiByteToWideChar is working only for codepage 1255 and not for 866</p>

<p>WideCharToMultiByte is not working for codepage 1251.</p>