Encoding.Convert Method (Encoding, Encoding, Byte[])

Converts an entire byte array from one encoding to another.

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)

public static byte[] Convert(
	Encoding srcEncoding,
	Encoding dstEncoding,
	byte[] bytes


Type: System.Text.Encoding
The encoding format of bytes.
Type: System.Text.Encoding
The target encoding format.
Type: System.Byte[]
The bytes to convert.

Return Value

Type: System.Byte[]
A byte array that contains the results of converting bytes from srcEncoding to dstEncoding.


srcEncoding is null.


dstEncoding is null.


bytes is null.


A fallback occurred (see Understanding Encodings for complete explanation).


A fallback occurred (see Understanding Encodings for complete explanation).

The following code example converts a string from one encoding to another.

using System;
using System.Text;

class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      string unicodeString = "This string contains the unicode character Pi (\u03a0)";

      // Create two different encodings.
      Encoding utf8 = Encoding.UTF8;
      Encoding unicode = Encoding.Unicode;

      // Convert the string into a byte[].
      byte[] unicodeBytes = unicode.GetBytes(unicodeString);

      // Perform the conversion from one encoding to the other.
      byte[] utf8Bytes = Encoding.Convert(unicode, utf8, unicodeBytes);

      // Convert the new byte[] into a char[] and then into a string.
      char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length)];
      utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0);
      string utf8String = new string(utf8Chars);

      // Display the strings created before and after the conversion.
      outputBlock.Text += String.Format("Original string: {0}", unicodeString) + "\n";
      outputBlock.Text += String.Format("Ascii converted string: {0}", utf8String) + "\n";
// The example displays the following output:
//    Original string: This string contains the unicode character Pi (Π)
//    Ascii converted string: This string contains the unicode character Pi (?)


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.