Questo argomento non è stato ancora valutato - Valuta questo argomento

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
)

Parameters

srcEncoding
Type: System.Text.Encoding
The encoding format of bytes.
dstEncoding
Type: System.Text.Encoding
The target encoding format.
bytes
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.
ExceptionCondition
ArgumentNullException

srcEncoding is null.

-or-

dstEncoding is null.

-or-

bytes is null.

DecoderFallbackException

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

EncoderFallbackException

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 (?)


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

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

Il documento è risultato utile?
(1500 caratteri rimanenti)

Aggiunte alla community

AGGIUNGI
Microsoft sta conducendo un sondaggio in linea per comprendere l'opinione degli utenti in merito al sito Web di MSDN. Se si sceglie di partecipare, quando si lascia il sito Web di MSDN verrà visualizzato il sondaggio in linea.

Si desidera partecipare?
© 2013 Microsoft. Tutti i diritti riservati.