Encoding.Convert 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將位元組陣列從一個編碼轉換成另一個編碼。
多載
Convert(Encoding, Encoding, Byte[], Int32, Int32) |
將位元組陣列中的位元組範圍從一個編碼轉換成另一個。 |
Convert(Encoding, Encoding, Byte[]) |
將整個位元組陣列從一個編碼轉換成另一個。 |
Convert(Encoding, Encoding, Byte[], Int32, Int32)
- 來源:
- Encoding.cs
- 來源:
- Encoding.cs
- 來源:
- 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 陣列,其中包含將 bytes
中位元組範圍從 srcEncoding
轉換成 dstEncoding
的結果。
例外狀況
index
和 count
未在位元組陣列中指定有效的範圍。
適用於
Convert(Encoding, Encoding, Byte[])
- 來源:
- Encoding.cs
- 來源:
- Encoding.cs
- 來源:
- 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 陣列,包含將 bytes
從 srcEncoding
轉換成 dstEncoding
的結果。
例外狀況
範例
下列範例會將 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 (?)