Encoding.Convert 方法

定义

将字节数组从一种编码转换为另一种。Converts a byte array from one encoding to another.

重载

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

将字节数组内某个范围的字节从一种编码转换为另一种编码。Converts a range of bytes in a byte array from one encoding to another.

Convert(Encoding, Encoding, Byte[])

将整个字节数组从一种编码转换为另一种编码。Converts an entire byte array from one encoding to another.

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

将字节数组内某个范围的字节从一种编码转换为另一种编码。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()

参数

srcEncoding
Encoding

源数组 bytes 的编码。The encoding of the source array, bytes.

dstEncoding
Encoding

输出数组的编码。The encoding of the output array.

bytes
Byte[]

要转换的字节数组。The array of bytes to convert.

index
Int32

要转换的 bytes 中第一个元素的索引。The index of the first element of bytes to convert.

count
Int32

要转换的字节数。The number of bytes to convert.

返回

Byte[]

一个 Byte 类型的数组,其中包含将 bytes 中某个范围的字节从 srcEncoding 转换为 dstEncoding 的结果。An array of type Byte containing the result of converting a range of bytes in bytes from srcEncoding to dstEncoding.

例外

srcEncodingnullsrcEncoding is null.

-or-

dstEncodingnulldstEncoding is null.

-or-

bytesnullbytes is null.

indexcount 不指定字节数组中的有效范围。index and count do not specify a valid range in the byte array.

发生回退(有关详细信息,请参阅采用 .NET 的字符编码A fallback occurred (for more information, see Character Encoding in .NET)

-和--and-

srcEncoding.srcEncoding. DecoderFallback 设置为 DecoderExceptionFallbackDecoderFallback is set to DecoderExceptionFallback.

发生回退(有关详细信息,请参阅采用 .NET 的字符编码A fallback occurred (for more information, see Character Encoding in .NET)

-和--and-

dstEncoding.dstEncoding. EncoderFallback 设置为 EncoderExceptionFallbackEncoderFallback is set to EncoderExceptionFallback.

适用于

Convert(Encoding, Encoding, Byte[])

将整个字节数组从一种编码转换为另一种编码。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()

参数

srcEncoding
Encoding

bytes 的编码格式。The encoding format of bytes.

dstEncoding
Encoding

目标编码格式。The target encoding format.

bytes
Byte[]

要转换的字节。The bytes to convert.

返回

Byte[]

Byte 类型的数组,其中包含将 bytessrcEncoding 转换为 dstEncoding 的结果。An array of type Byte containing the results of converting bytes from srcEncoding to dstEncoding.

例外

srcEncodingnullsrcEncoding is null.

-or-

dstEncodingnulldstEncoding is null.

-or-

bytesnullbytes is null.

发生回退(有关详细信息,请参阅采用 .NET 的字符编码A fallback occurred (for more information, see Character Encoding in .NET)

-和--and-

srcEncoding.srcEncoding. DecoderFallback 设置为 DecoderExceptionFallbackDecoderFallback is set to DecoderExceptionFallback.

发生回退(有关详细信息,请参阅采用 .NET 的字符编码A fallback occurred (for more information, see Character Encoding in .NET)

-和--and-

dstEncoding.dstEncoding. EncoderFallback 设置为 EncoderExceptionFallbackEncoderFallback is set to EncoderExceptionFallback.

示例

下面的示例将 Unicode 编码的字符串转换为 ASCII 编码的字符串。The following example converts a Unicode-encoded string to an ASCII-encoded string. 由于属性返回的 ASCII 编码对象 ASCII 使用替换回退,并且 pi 字符不是 ASCII 字符集的一部分,因此 pi 字符将替换为问号,如示例中的输出所示。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 (?)

适用于