This documentation is archived and is not being maintained.

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

Converts an entire byte array from one encoding to another.

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

'Declaration
Public Shared Function Convert ( _
	srcEncoding As Encoding, _
	dstEncoding As Encoding, _
	bytes As Byte() _
) As Byte()
'Usage
Dim srcEncoding As Encoding
Dim dstEncoding As Encoding
Dim bytes As Byte()
Dim returnValue As Byte()

returnValue = Encoding.Convert(srcEncoding, dstEncoding, bytes)
public static byte[] Convert (
	Encoding srcEncoding, 
	Encoding dstEncoding, 
	byte[] bytes
)
public static function Convert (
	srcEncoding : Encoding, 
	dstEncoding : Encoding, 
	bytes : byte[]
) : byte[]

Parameters

srcEncoding

The encoding format of bytes.

dstEncoding

The target encoding format.

bytes

The bytes to convert.

Return Value

An array of type Byte containing the results of converting bytes from srcEncoding to dstEncoding.

Exception typeCondition

ArgumentNullException

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

-or-

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

-or-

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

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

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

package ConvertExample; 

import System.*;
import System.Text.*;

class ConvertExampleClass
{
    public static void main(String[] args)
    {
        String unicodeString = 
                "This string contains the unicode character Pi(\u03a0)";

        // Create two different encodings.
        Encoding ascii = Encoding.get_ASCII();
        Encoding unicode = Encoding.get_Unicode();

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

        // Perform the conversion from one encoding to the other.
        ubyte 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);
    } //main
} //ConvertExampleClass 

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: