Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

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
)
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.

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);
      }
   }
}

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.