Encoding.Convert Метод

Определение

Преобразует массив байтов из одной кодировки в другую.

Перегрузки

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

Преобразует диапазон байтов в массиве байтов из одной кодировки в другую.

Convert(Encoding, Encoding, Byte[])

Преобразует весь массив байтов из одной кодировки в другую.

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

Преобразует диапазон байтов в массиве байтов из одной кодировки в другую.

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

Параметры

srcEncoding
Encoding

Кодировка исходного массива, bytes.

dstEncoding
Encoding

Кодировка выходного массива.

bytes
Byte[]

Преобразуемый массив байтов.

index
Int32

Индекс первого элемента преобразуемого массива байтов bytes.

count
Int32

Число байтов, которые требуется преобразовать.

Возвращаемое значение

Byte[]

Массив типа Byte, содержащий результат преобразования диапазона байтов из массива bytes из srcEncoding в dstEncoding.

Исключения

srcEncoding имеет значение null.

-или-

dstEncoding имеет значение null.

-или-

bytes имеет значение null.

index и count не определяют допустимый диапазон в массиве байтов.

Произошел откат (см. сведения о кодировке символов в .NET)

  • и -

srcEncoding. Параметру DecoderFallback задается значение DecoderExceptionFallback.

Произошел откат (см. сведения о кодировке символов в .NET)

  • и -

dstEncoding. Параметру EncoderFallback задается значение EncoderExceptionFallback.

Применяется к

Convert(Encoding, Encoding, Byte[])

Преобразует весь массив байтов из одной кодировки в другую.

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

Параметры

srcEncoding
Encoding

Формат кодировки параметра bytes.

dstEncoding
Encoding

Целевой формат кодировки.

bytes
Byte[]

Преобразуемые байты.

Возвращаемое значение

Byte[]

Массив типа Byte, содержащий результаты преобразования bytes из srcEncoding в dstEncoding.

Исключения

srcEncoding имеет значение null.

-или-

dstEncoding имеет значение null.

-или-

bytes имеет значение null.

Произошел откат (см. сведения о кодировке символов в .NET)

  • и -

srcEncoding. Параметру DecoderFallback задается значение DecoderExceptionFallback.

Произошел откат (см. сведения о кодировке символов в .NET)

  • и -

dstEncoding. Параметру EncoderFallback задается значение EncoderExceptionFallback.

Примеры

В следующем примере строка в кодировке Юникод преобразуется в строку в кодировке ASCII. Поскольку объект кодировки ASCII, возвращаемый ASCII свойством, использует замену замены, а символ PI не является частью набора символов ASCII, символ PI заменяется вопросительным знаком, как показано в выходных данных примера.

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

Применяется к