Encoding.Convert Metoda

Definicja

Konwertuje tablicę bajtową z jednego kodowania na inną.Converts a byte array from one encoding to another.

Przeciążenia

Convert(Encoding, Encoding, Byte[], Int32, Int32)

Konwertuje zakres bajtów w tablicy bajtów z jednego kodowania na inny.Converts a range of bytes in a byte array from one encoding to another.

Convert(Encoding, Encoding, Byte[])

Konwertuje całą tablicę bajtową z jednego kodowania na inną.Converts an entire byte array from one encoding to another.

Convert(Encoding, Encoding, Byte[], Int32, Int32)

Konwertuje zakres bajtów w tablicy bajtów z jednego kodowania na inny.Converts a range of bytes in a byte array from one encoding to another.

public:
 static cli::array <System::Byte> ^ Convert(System::Text::Encoding ^ srcEncoding, System::Text::Encoding ^ dstEncoding, cli::array <System::Byte> ^ bytes, int index, int count);
public static byte[] Convert (System.Text.Encoding srcEncoding, System.Text.Encoding dstEncoding, byte[] bytes, int index, int count);
static member Convert : System.Text.Encoding * System.Text.Encoding * byte[] * int * int -> byte[]
Public Shared Function Convert (srcEncoding As Encoding, dstEncoding As Encoding, bytes As Byte(), index As Integer, count As Integer) As Byte()

Parametry

srcEncoding
Encoding

Kodowanie tablicy źródłowej bytes .The encoding of the source array, bytes.

dstEncoding
Encoding

Kodowanie tablicy wyjściowej.The encoding of the output array.

bytes
Byte[]

Tablica bajtów do przekonwertowania.The array of bytes to convert.

index
Int32

Indeks pierwszego elementu bytes do przekonwertowania.The index of the first element of bytes to convert.

count
Int32

Liczba bajtów do przekonwertowania.The number of bytes to convert.

Zwraca

Byte[]

Tablica typu Byte zawierającego wynik konwersji zakresu bajtów w bytes z srcEncoding do dstEncoding .An array of type Byte containing the result of converting a range of bytes in bytes from srcEncoding to dstEncoding.

Wyjątki

srcEncoding to null.srcEncoding is null.

-lub--or-

dstEncoding to null.dstEncoding is null.

-lub--or-

bytes to null.bytes is null.

index i count nie określaj prawidłowego zakresu w tablicy bajtów.index and count do not specify a valid range in the byte array.

Wystąpiła rezerwa (Aby uzyskać więcej informacji, zobacz kodowanie znaków w programie .NET).A fallback occurred (for more information, see Character Encoding in .NET)

lub-and-

srcEncoding.srcEncoding. DecoderFallback jest ustawiony na DecoderExceptionFallback .DecoderFallback is set to DecoderExceptionFallback.

Wystąpiła rezerwa (Aby uzyskać więcej informacji, zobacz kodowanie znaków w programie .NET).A fallback occurred (for more information, see Character Encoding in .NET)

lub-and-

dstEncoding.dstEncoding. EncoderFallback jest ustawiony na EncoderExceptionFallback .EncoderFallback is set to EncoderExceptionFallback.

Dotyczy

Convert(Encoding, Encoding, Byte[])

Konwertuje całą tablicę bajtową z jednego kodowania na inną.Converts an entire byte array from one encoding to another.

public:
 static cli::array <System::Byte> ^ Convert(System::Text::Encoding ^ srcEncoding, System::Text::Encoding ^ dstEncoding, cli::array <System::Byte> ^ bytes);
public static byte[] Convert (System.Text.Encoding srcEncoding, System.Text.Encoding dstEncoding, byte[] bytes);
static member Convert : System.Text.Encoding * System.Text.Encoding * byte[] -> byte[]
Public Shared Function Convert (srcEncoding As Encoding, dstEncoding As Encoding, bytes As Byte()) As Byte()

Parametry

srcEncoding
Encoding

Format kodowania bytes .The encoding format of bytes.

dstEncoding
Encoding

Docelowy format kodowania.The target encoding format.

bytes
Byte[]

Bajty do przekonwertowania.The bytes to convert.

Zwraca

Byte[]

Tablica typu Byte zawierającego wyniki konwersji bytes z srcEncoding do dstEncoding .An array of type Byte containing the results of converting bytes from srcEncoding to dstEncoding.

Wyjątki

srcEncoding to null.srcEncoding is null.

-lub--or-

dstEncoding to null.dstEncoding is null.

-lub--or-

bytes to null.bytes is null.

Wystąpiła rezerwa (Aby uzyskać więcej informacji, zobacz kodowanie znaków w programie .NET).A fallback occurred (for more information, see Character Encoding in .NET)

lub-and-

srcEncoding.srcEncoding. DecoderFallback jest ustawiony na DecoderExceptionFallback .DecoderFallback is set to DecoderExceptionFallback.

Wystąpiła rezerwa (Aby uzyskać więcej informacji, zobacz kodowanie znaków w programie .NET).A fallback occurred (for more information, see Character Encoding in .NET)

lub-and-

dstEncoding.dstEncoding. EncoderFallback jest ustawiony na EncoderExceptionFallback .EncoderFallback is set to EncoderExceptionFallback.

Przykłady

Poniższy przykład konwertuje ciąg zakodowany w formacie Unicode na ciąg zakodowany w formacie ASCII.The following example converts a Unicode-encoded string to an ASCII-encoded string. Ponieważ obiekt kodowania ASCII zwrócony przez ASCII Właściwość używa rezerwy zastępczej, a znak pi nie jest częścią zestawu znaków ASCII, znak Pi jest zastępowany znakiem zapytania, ponieważ dane wyjściowe z przykładu są wyświetlane.Because the ASCII encoding object returned by the ASCII property uses replacement fallback and the Pi character is not part of the ASCII character set, the Pi character is replaced with a question mark, as the output from the example shows.

using namespace System;
using namespace System::Text;

int 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 array.
   array<Byte>^unicodeBytes = unicode->GetBytes( unicodeString );
   
   // Perform the conversion from one encoding to the other.
   array<Byte>^asciiBytes = Encoding::Convert( unicode, ascii, unicodeBytes );
   
   // Convert the new Byte into[] a char and[] then into a string.
   array<Char>^asciiChars = gcnew array<Char>(ascii->GetCharCount( asciiBytes, 0, asciiBytes->Length ));
   ascii->GetChars( asciiBytes, 0, asciiBytes->Length, asciiChars, 0 );
   String^ asciiString = gcnew String( asciiChars );
   
   // Display the strings created before and after the conversion.
   Console::WriteLine( "Original String*: {0}", unicodeString );
   Console::WriteLine( "Ascii converted String*: {0}", asciiString );
}
// 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
{
   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 array.
      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.
      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);
   }
}
// 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 (?)
Imports System.Text

Class Example
   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 array.
      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 array into a char array and then into a string.
      Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1) 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
' 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 (?)

Dotyczy