Export (0) Print
Expand All

Encoding.Convert Method

Converts a byte array from one encoding to another.

Overload List

Converts a byte array from one encoding to another.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Convert(Encoding, Encoding, Byte()) As Byte()
[C#] public static byte[] Convert(Encoding, Encoding, byte[]);
[C++] public: static unsigned char Convert(Encoding*, Encoding*, unsigned char __gc[]) __gc[];
[JScript] public static function Convert(Encoding, Encoding, Byte[]) : Byte[];

Converts a range of bytes in a byte array from one encoding to another.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Convert(Encoding, Encoding, Byte(), Integer, Integer) As Byte()
[C#] public static byte[] Convert(Encoding, Encoding, byte[], int, int);
[C++] public: static unsigned char Convert(Encoding*, Encoding*, unsigned char __gc[], int, int) __gc[];
[JScript] public static function Convert(Encoding, Encoding, Byte[], int, int) : Byte[];

Example

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Convert. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Text
Imports Microsoft.VisualBasic

Namespace Convert_Example
    Class MyConvertExampleClass
        Shared Sub Main()
            Dim unicodeString As String = "This string contains the unicode character Pi(" & ChrW(&H03A0) & ")"

            ' Create two different encodings.
            Dim ascii As Encoding = Encoding.ASCII
            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 asciiBytes As Byte() = Encoding.Convert([unicode], ascii, unicodeBytes)

            ' Convert the new byte[] into a char[] and then into a string.
            ' This is a slightly different approach to converting to illustrate
            ' the use of GetCharCount/GetChars.
            Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)) As Char
            ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
            Dim asciiString As New String(asciiChars)

            ' Display the strings created before and after the conversion.
            Console.WriteLine("Original string: {0}", unicodeString)
            Console.WriteLine("Ascii converted string: {0}", asciiString)
        End Sub
    End Class
End Namespace

[C#] 
using System;
using System.Text;

namespace ConvertExample
{
   class ConvertExampleClass
   {
      static void Main()
      {
         string unicodeString = "This string contains the unicode character Pi(\u03a0)";

         // Create two different encodings.
         Encoding ascii = Encoding.ASCII;
         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[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
            
         // Convert the new byte[] into a char[] and then into a string.
         // This is a slightly different approach to converting to illustrate
         // the use of GetCharCount/GetChars.
         char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         string asciiString = new string(asciiChars);

         // Display the strings created before and after the conversion.
         Console.WriteLine("Original string: {0}", unicodeString);
         Console.WriteLine("Ascii converted string: {0}", asciiString);
      }
   }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

int main()
{
   String* unicodeString = S"This string contains the unicode character Pi(\u03a0)";

   // Create two different encodings.
   Encoding * ascii = Encoding::ASCII;
   Encoding * unicode = Encoding::Unicode;

   // Convert the string into a Byte->Item[].
   Byte unicodeBytes[] = unicode -> GetBytes(unicodeString);

   // Perform the conversion from one encoding to the other.
   Byte asciiBytes[] = Encoding::Convert(unicode, ascii, unicodeBytes);

   // Convert the new Byte into[] a char and[] then into a string.
   // This is a slightly different approach to converting to illustrate
   // the use of GetCharCount/GetChars.
   Char asciiChars[] = new Char[ascii -> GetCharCount(asciiBytes, 0, asciiBytes -> Length)];
   ascii -> GetChars(asciiBytes, 0, asciiBytes->Length, asciiChars, 0);
   String* asciiString = new String(asciiChars);

   // Display the strings created before and after the conversion.
   Console::WriteLine(S"Original String*: {0}", unicodeString);
   Console::WriteLine(S"Ascii converted String*: {0}", asciiString);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Encoding Class | Encoding Members | System.Text Namespace

Show:
© 2014 Microsoft