แก้ไข

แชร์ผ่าน


Encoding.Convert Method

Definition

Converts a byte array from one encoding to another.

Overloads

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)

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

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

Parameters

srcEncoding
Encoding

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

The index of the first element of bytes to convert.

count
Int32

The number of bytes to convert.

Returns

Byte[]

An array of type Byte containing the result of converting a range of bytes in bytes from srcEncoding to dstEncoding.

Exceptions

srcEncoding is null.

-or-

dstEncoding is null.

-or-

bytes is null.

index and count do not specify a valid range in the byte array.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

srcEncoding. DecoderFallback is set to DecoderExceptionFallback.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

dstEncoding. EncoderFallback is set to EncoderExceptionFallback.

Applies to

Convert(Encoding, Encoding, Byte[])

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

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

Parameters

srcEncoding
Encoding

The encoding format of bytes.

dstEncoding
Encoding

The target encoding format.

bytes
Byte[]

The bytes to convert.

Returns

Byte[]

An array of type Byte containing the results of converting bytes from srcEncoding to dstEncoding.

Exceptions

srcEncoding is null.

-or-

dstEncoding is null.

-or-

bytes is null.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

srcEncoding. DecoderFallback is set to DecoderExceptionFallback.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

dstEncoding. EncoderFallback is set to EncoderExceptionFallback.

Examples

The following example converts a Unicode-encoded string to an ASCII-encoded string. 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 (?)

Applies to