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

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts an entire byte array from one encoding to another.

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

Syntax

'Declaration
Public Shared Function Convert ( _
    srcEncoding As Encoding, _
    dstEncoding As Encoding, _
    bytes As Byte() _
) As Byte()
public static byte[] Convert(
    Encoding srcEncoding,
    Encoding dstEncoding,
    byte[] bytes
)

Parameters

  • bytes
    Type: array<System.Byte[]
    The bytes to convert.

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

srcEncoding is nulla null reference (Nothing in Visual Basic).

-or-

dstEncoding is nulla null reference (Nothing in Visual Basic).

-or-

bytes is nulla null reference (Nothing in Visual Basic).

DecoderFallbackException

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

EncoderFallbackException

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

Examples

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

Imports System.Text

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim unicodeString As String = "This string contains the unicode character Pi(" & ChrW(&H3A0) & ")"

      ' Create two different encodings.
      Dim utf8 As Encoding = Encoding.UTF8
      Dim unicode As Encoding = Encoding.Unicode

      ' Convert the string into a byte[].
      Dim unicodeBytes As Byte() = unicode.GetBytes(unicodeString)

      ' Perform the conversion from one encoding to the other.
      Dim utf8Bytes As Byte() = Encoding.Convert(unicode, utf8, unicodeBytes)

      ' Convert the new byte[] into a char[] and then into a string.
      Dim utf8Chars(utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length) - 1) As Char
      utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0)
      Dim utf8String As New String(utf8Chars)

      ' Display the strings created before and after the conversion.
      outputBlock.Text += String.Format("Original string: {0}", unicodeString) & vbCrLf
      outputBlock.Text += String.Format("Ascii converted string: {0}", utf8String) & vbCrLf
   End Sub
End Class
' 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 (?)
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 (?)

Version Information

Silverlight

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

Platforms

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