Encoding.Convert 方法

定义

将字节数组从一个编码转换为另一个编码。

重载

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

将字节数组中的字节范围从一个编码转换为另一个编码。

Convert(Encoding, Encoding, Byte[])

将整个字节数组从一个编码转换为另一个编码。

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

Source:
Encoding.cs
Source:
Encoding.cs
Source:
Encoding.cs

将字节数组中的字节范围从一个编码转换为另一个编码。

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 数组,其中包含将一系列字节从 srcEncoding 转换为 dstEncoding的结果(以 bytes 为单位)。

例外

srcEncoding null

-或-

dstEncoding null

-或-

bytes null

indexcount 未在字节数组中指定有效范围。

发生回退(有关详细信息,请参阅 .NET中的 字符编码)

-和-

srcEncoding。DecoderFallback 设置为 DecoderExceptionFallback

发生回退(有关详细信息,请参阅 .NET中的 字符编码)

-和-

dstEncoding。EncoderFallback 设置为 EncoderExceptionFallback

适用于

Convert(Encoding, Encoding, Byte[])

Source:
Encoding.cs
Source:
Encoding.cs
Source:
Encoding.cs

将整个字节数组从一个编码转换为另一个编码。

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 数组,其中包含将 bytessrcEncoding 转换为 dstEncoding的结果。

例外

srcEncoding null

-或-

dstEncoding null

-或-

bytes null

发生回退(有关详细信息,请参阅 .NET中的 字符编码)

-和-

srcEncoding。DecoderFallback 设置为 DecoderExceptionFallback

发生回退(有关详细信息,请参阅 .NET中的 字符编码)

-和-

dstEncoding。EncoderFallback 设置为 EncoderExceptionFallback

示例

以下示例将 Unicode 编码的字符串转换为 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 (?)

适用于