UTF7Encoding 類別

定義

代表 Unicode 字元的 UTF-7 編碼方式。Represents a UTF-7 encoding of Unicode characters.

public ref class UTF7Encoding : System::Text::Encoding
public class UTF7Encoding : System.Text.Encoding
[System.Serializable]
public class UTF7Encoding : System.Text.Encoding
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class UTF7Encoding : System.Text.Encoding
type UTF7Encoding = class
    inherit Encoding
Public Class UTF7Encoding
Inherits Encoding
繼承
UTF7Encoding
屬性

範例

下列程式碼範例示範如何使用 UTF7Encoding 來編碼 Unicode 字元的字串,並將其儲存在位元組陣列中。The following code example demonstrates how to use a UTF7Encoding to encode a string of Unicode characters and store them in a byte array. 請注意,當位元組陣列解碼回字串時,不會遺失任何資料。Notice that when the byte array is decoded back to a string, no data is lost.

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   
   // Create a UTF-7 encoding.
   UTF7Encoding^ utf7 = gcnew UTF7Encoding;
   
   // A Unicode string with two characters outside a 7-bit code range.
   String^ unicodeString = L"This Unicode string contains two characters with codes outside a 7-bit code range, Pi (\u03a0) and Sigma (\u03a3).";
   Console::WriteLine( "Original string:" );
   Console::WriteLine( unicodeString );
   
   // Encode the string.
   array<Byte>^encodedBytes = utf7->GetBytes( unicodeString );
   Console::WriteLine();
   Console::WriteLine( "Encoded bytes:" );
   IEnumerator^ myEnum = encodedBytes->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Byte b = safe_cast<Byte>(myEnum->Current);
      Console::Write( "[{0}]", b );
   }

   Console::WriteLine();
   
   // Decode bytes back to string.
   // Notice Pi and Sigma characters are still present.
   String^ decodedString = utf7->GetString( encodedBytes );
   Console::WriteLine();
   Console::WriteLine( "Decoded bytes:" );
   Console::WriteLine( decodedString );
}

using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        // Create a UTF-7 encoding.
        UTF7Encoding utf7 = new UTF7Encoding();
        
        // A Unicode string with two characters outside a 7-bit code range.
        String unicodeString =
            "This Unicode string contains two characters " +
            "with codes outside a 7-bit code range, " +
            "Pi (\u03a0) and Sigma (\u03a3).";
        Console.WriteLine("Original string:");
        Console.WriteLine(unicodeString);

        // Encode the string.
        Byte[] encodedBytes = utf7.GetBytes(unicodeString);
        Console.WriteLine();
        Console.WriteLine("Encoded bytes:");
        foreach (Byte b in encodedBytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();
        
        // Decode bytes back to string.
        // Notice Pi and Sigma characters are still present.
        String decodedString = utf7.GetString(encodedBytes);
        Console.WriteLine();
        Console.WriteLine("Decoded bytes:");
        Console.WriteLine(decodedString);
    }
}
Imports System.Text
Imports Microsoft.VisualBasic.Strings

Class UTF7EncodingExample
    
    Public Shared Sub Main()
        ' Create a UTF-7 encoding.
        Dim utf7 As New UTF7Encoding()
        
        ' A Unicode string with two characters outside a 7-bit code range.
        Dim unicodeString As String = _
            "This Unicode string contains two characters " & _
            "with codes outside a 7-bit code range, " & _
            "Pi (" & ChrW(928) & ") and Sigma (" & ChrW(931) & ")."
        Console.WriteLine("Original string:")
        Console.WriteLine(unicodeString)
        
        ' Encode the string.
        Dim encodedBytes As Byte() = utf7.GetBytes(unicodeString)
        Console.WriteLine()
        Console.WriteLine("Encoded bytes:")
        Dim b As Byte
        For Each b In  encodedBytes
            Console.Write("[{0}]", b)
        Next b
        Console.WriteLine()
        
        ' Decode bytes back to string.
        ' Notice Pi and Sigma characters are still present.
        Dim decodedString As String = utf7.GetString(encodedBytes)
        Console.WriteLine()
        Console.WriteLine("Decoded bytes:")
        Console.WriteLine(decodedString)
    End Sub
End Class

備註

編碼是將一組 Unicode 字元轉換成位元組序列的處理程序。Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. 解碼是將編碼的位元組序列轉換成一組 Unicode 字元的過程。Decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters.

UTF-7 編碼將 Unicode 字元表示為7位 ASCII 字元的序列。The UTF-7 encoding represents Unicode characters as sequences of 7-bit ASCII characters. 這種編碼方式可支援所需的特定通訊協定,通常是電子郵件或新聞群組通訊協定。This encoding supports certain protocols for which it is required, most often email or newsgroup protocols. 由於 UTF-7 並不特別安全,而且大部分的新式系統都允許8位編碼,因此,UTF-8 一般應該慣用 utf-8。Since UTF-7 is not particularly secure or robust, and most modern systems allow 8-bit encodings, UTF-8 should normally be preferred to UTF-7.

注意

UTF7Encoding 不會提供錯誤偵測。UTF7Encoding does not provide error detection. 基於安全性理由,應用程式應該使用 UTF8EncodingUnicodeEncodingUTF32Encoding 並啟用錯誤偵測。For security reasons, the application should use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

如需 System.Text所支援的 UTFs 和其他編碼的詳細資訊,請參閱.NET Framework 中的字元編碼For more information about the UTFs and other encodings supported by System.Text, see Character Encoding in the .NET Framework.

GetByteCount 方法會決定編碼一組 Unicode 字元所產生的位元組數目,而 GetBytes 方法則會執行實際的編碼。The GetByteCount method determines how many bytes result in encoding a set of Unicode characters, and the GetBytes method performs the actual encoding.

同樣地,GetCharCount 方法會決定有多少字元會導致解碼位元組序列,而 GetCharsGetString 方法會執行實際的解碼。Likewise, the GetCharCount method determines how many characters result in decoding a sequence of bytes, and the GetChars and GetString methods perform the actual decoding.

UTF7Encoding 對應至 Windows 字碼頁65000。UTF7Encoding corresponds to the Windows code page 65000.

注意

如果物件已使用不同的 .NET Framework 版本進行序列化和還原序列化,則不會保留 UTF-7 編碼物件的狀態。The state of a UTF-7 encoded object is not preserved if the object is serialized and deserialized using different .NET Framework versions.

建構函式

UTF7Encoding()

初始化 UTF7Encoding 類別的新執行個體。Initializes a new instance of the UTF7Encoding class.

UTF7Encoding(Boolean)

初始化 UTF7Encoding 類別的新執行個體。Initializes a new instance of the UTF7Encoding class. 參數可指定是否允許使用選擇性字元。A parameter specifies whether to allow optional characters.

屬性

BodyName

在衍生類別中覆寫時,取得可以與郵件代理程式主體標籤一起使用的目前編碼方式名稱。When overridden in a derived class, gets a name for the current encoding that can be used with mail agent body tags.

(繼承來源 Encoding)
CodePage

在衍生類別中覆寫時,取得目前 Encoding 的字碼頁識別項。When overridden in a derived class, gets the code page identifier of the current Encoding.

(繼承來源 Encoding)
DecoderFallback

取得或設定目前 DecoderFallback 物件的 Encoding 物件。Gets or sets the DecoderFallback object for the current Encoding object.

(繼承來源 Encoding)
EncoderFallback

取得或設定目前 EncoderFallback 物件的 Encoding 物件。Gets or sets the EncoderFallback object for the current Encoding object.

(繼承來源 Encoding)
EncodingName

在衍生類別中覆寫時,取得目前編碼方式的人們可讀取 (Human-Readable) 的描述。When overridden in a derived class, gets the human-readable description of the current encoding.

(繼承來源 Encoding)
HeaderName

在衍生類別中覆寫時,取得可以與郵件代理程式標頭標籤一起使用的目前編碼方式名稱。When overridden in a derived class, gets a name for the current encoding that can be used with mail agent header tags.

(繼承來源 Encoding)
IsBrowserDisplay

在衍生類別中覆寫時,取得值,指出瀏覽器用戶端是否可以使用目前的編碼方式來顯示內容。When overridden in a derived class, gets a value indicating whether the current encoding can be used by browser clients for displaying content.

(繼承來源 Encoding)
IsBrowserSave

在衍生類別中覆寫時,取得值,指出瀏覽器用戶端是否可以使用目前的編碼方式來儲存內容。When overridden in a derived class, gets a value indicating whether the current encoding can be used by browser clients for saving content.

(繼承來源 Encoding)
IsMailNewsDisplay

在衍生類別中覆寫時,取得值,指出郵件和新聞用戶端是否可以使用目前的編碼方式來顯示內容。When overridden in a derived class, gets a value indicating whether the current encoding can be used by mail and news clients for displaying content.

(繼承來源 Encoding)
IsMailNewsSave

在衍生類別中覆寫時,取得值,指出郵件和新聞用戶端是否可以使用目前的編碼方式來儲存內容。When overridden in a derived class, gets a value indicating whether the current encoding can be used by mail and news clients for saving content.

(繼承來源 Encoding)
IsReadOnly

在衍生類別中覆寫時,取得值,指出目前的編碼方式是否為唯讀。When overridden in a derived class, gets a value indicating whether the current encoding is read-only.

(繼承來源 Encoding)
IsSingleByte

在衍生類別中覆寫時,取得值,指出目前的編碼方式是否使用單一位元組字碼指標。When overridden in a derived class, gets a value indicating whether the current encoding uses single-byte code points.

(繼承來源 Encoding)
Preamble

在衍生類別中覆寫時,傳回範圍,其包含指定所用編碼方式的位元組序列。When overridden in a derived class, returns a span containing the sequence of bytes that specifies the encoding used.

(繼承來源 Encoding)
WebName

在衍生類別中覆寫時,若要取得目前的編碼方式,請取得向 Internet Assigned Numbers Authority (IANA) 註冊的名稱。When overridden in a derived class, gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.

(繼承來源 Encoding)
WindowsCodePage

在衍生類別中覆寫時,請取得最能符合目前編碼方式的 Windows 作業系統字碼頁。When overridden in a derived class, gets the Windows operating system code page that most closely corresponds to the current encoding.

(繼承來源 Encoding)

方法

Clone()

在衍生類別中覆寫時,會建立目前 Encoding 物件的淺層複本。When overridden in a derived class, creates a shallow copy of the current Encoding object.

(繼承來源 Encoding)
Equals(Object)

取得值,指出指定的物件是否等於目前的 UTF7Encoding 物件。Gets a value indicating whether the specified object is equal to the current UTF7Encoding object.

GetByteCount(Char*, Int32)

計算將起始於指定字元指標的一組字元編碼所產生的位元組數目。Calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer.

GetByteCount(Char[])

在衍生類別中覆寫時,計算編碼指定字元陣列中所有字元所產生的位元組數目。When overridden in a derived class, calculates the number of bytes produced by encoding all the characters in the specified character array.

(繼承來源 Encoding)
GetByteCount(Char[], Int32, Int32)

計算將指定字元陣列中的一組字元編碼所產生的位元組數目。Calculates the number of bytes produced by encoding a set of characters from the specified character array.

GetByteCount(ReadOnlySpan<Char>)

在衍生類別中覆寫時,計算藉由編碼指定字元範圍中字元所產生的位元組數。When overridden in a derived class, calculates the number of bytes produced by encoding the characters in the specified character span.

(繼承來源 Encoding)
GetByteCount(String)

編碼指定 String 物件中的字元,從而計算所產生的位元組數。Calculates the number of bytes produced by encoding the characters in the specified String object.

GetByteCount(String, Int32, Int32)

在衍生類別中覆寫時,計算藉由從指定的字串編碼一組字元所產生的位元組數。When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters from the specified string.

(繼承來源 Encoding)
GetBytes(Char*, Int32, Byte*, Int32)

將起始於指定字元指標的一組字元編碼成位元組序列;儲存該位元組序列時,係以指定的位元組指標為起始點。Encodes a set of characters starting at the specified character pointer into a sequence of bytes that are stored starting at the specified byte pointer.

GetBytes(Char[])

在衍生類別中覆寫時,將指定字元陣列中的所有字元編碼成位元組序列。When overridden in a derived class, encodes all the characters in the specified character array into a sequence of bytes.

(繼承來源 Encoding)
GetBytes(Char[], Int32, Int32)

在衍生類別中覆寫時,將指定字元陣列中的一組字元編碼成位元組序列。When overridden in a derived class, encodes a set of characters from the specified character array into a sequence of bytes.

(繼承來源 Encoding)
GetBytes(Char[], Int32, Int32, Byte[], Int32)

將指定字元陣列中的一組字元編碼成指定的位元組陣列。Encodes a set of characters from the specified character array into the specified byte array.

GetBytes(ReadOnlySpan<Char>, Span<Byte>)

在衍生類別中覆寫時,從指定的唯讀範圍將一組字元編碼到位元組範圍。When overridden in a derived class, encodes into a span of bytes a set of characters from the specified read-only span.

(繼承來源 Encoding)
GetBytes(String)

在衍生類別中覆寫時,將指定字串中的所有字元編碼成位元組序列。When overridden in a derived class, encodes all the characters in the specified string into a sequence of bytes.

(繼承來源 Encoding)
GetBytes(String, Int32, Int32)

在衍生類別中覆寫時,從指定字串中指定的 index 開始,將 count 指定的字元數編碼到位元組陣列。When overridden in a derived class, encodes into an array of bytes the number of characters specified by count in the specified string, starting from the specified index.

(繼承來源 Encoding)
GetBytes(String, Int32, Int32, Byte[], Int32)

將指定 String 中的一組字元編碼成指定的位元組陣列。Encodes a set of characters from the specified String into the specified byte array.

GetCharCount(Byte*, Int32)

計算將起始於指定位元組指標的位元組序列解碼所產生的字元數。Calculates the number of characters produced by decoding a sequence of bytes starting at the specified byte pointer.

GetCharCount(Byte[])

在衍生類別中覆寫時,計算解碼指定位元組陣列中所有位元組所產生的字元數目。When overridden in a derived class, calculates the number of characters produced by decoding all the bytes in the specified byte array.

(繼承來源 Encoding)
GetCharCount(Byte[], Int32, Int32)

計算將指定位元組陣列中的位元組序列解碼所產生的字元數。Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.

GetCharCount(ReadOnlySpan<Byte>)

在衍生類別中覆寫時,計算藉由解碼所提供唯讀位元組範圍時產生的字元數。When overridden in a derived class, calculates the number of characters produced by decoding the provided read-only byte span.

(繼承來源 Encoding)
GetChars(Byte*, Int32, Char*, Int32)

將起始於指定位元組指標的位元組序列解碼成一組字元;儲存該組字元時,係以指定的字元指標為起始點。Decodes a sequence of bytes starting at the specified byte pointer into a set of characters that are stored starting at the specified character pointer.

GetChars(Byte[])

在衍生類別中覆寫時,將指定位元組陣列中的所有位元組解碼成一組字元。When overridden in a derived class, decodes all the bytes in the specified byte array into a set of characters.

(繼承來源 Encoding)
GetChars(Byte[], Int32, Int32)

在衍生類別中覆寫時,將指定位元組陣列中的位元組序列解碼成一組字元。When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a set of characters.

(繼承來源 Encoding)
GetChars(Byte[], Int32, Int32, Char[], Int32)

將指定位元組陣列中的位元組序列解碼成指定的字元陣列。Decodes a sequence of bytes from the specified byte array into the specified character array.

GetChars(ReadOnlySpan<Byte>, Span<Char>)

在衍生類別中覆寫時,將指定唯讀位元組範圍中的所有位元組解碼成字元範圍。When overridden in a derived class, decodes all the bytes in the specified read-only byte span into a character span.

(繼承來源 Encoding)
GetDecoder()

取得可以將以 UTF-7 編碼的位元組序列轉換成 Unicode 字元序列的解碼器。Obtains a decoder that converts a UTF-7 encoded sequence of bytes into a sequence of Unicode characters.

GetEncoder()

取得可以將 Unicode 字元序列轉換成以 UTF-7 編碼的位元組序列的編碼器。Obtains an encoder that converts a sequence of Unicode characters into a UTF-7 encoded sequence of bytes.

GetHashCode()

傳回目前 UTF7Encoding 物件的雜湊碼。Returns the hash code for the current UTF7Encoding object.

GetMaxByteCount(Int32)

計算將指定數目的字元編碼所產生的最大位元組數目。Calculates the maximum number of bytes produced by encoding the specified number of characters.

GetMaxCharCount(Int32)

計算將指定數目的位元組解碼所產生的最大字元數。Calculates the maximum number of characters produced by decoding the specified number of bytes.

GetPreamble()

在衍生類別中覆寫時,傳回可指定所用編碼方式的位元組序列。When overridden in a derived class, returns a sequence of bytes that specifies the encoding used.

(繼承來源 Encoding)
GetString(Byte*, Int32)

在衍生類別中覆寫時,將指定位址開頭之指定數目的位元組解碼為字串。When overridden in a derived class, decodes a specified number of bytes starting at a specified address into a string.

(繼承來源 Encoding)
GetString(Byte[])

在衍生類別中覆寫時,將指定位元組陣列中的所有位元組解碼成字串。When overridden in a derived class, decodes all the bytes in the specified byte array into a string.

(繼承來源 Encoding)
GetString(Byte[], Int32, Int32)

將位元組陣列中的某一段位元組範圍解碼成字串。Decodes a range of bytes from a byte array into a string.

GetString(ReadOnlySpan<Byte>)

在衍生類別中覆寫時,將指定位元組範圍中的所有位元組解碼成字串。When overridden in a derived class, decodes all the bytes in the specified byte span into a string.

(繼承來源 Encoding)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsAlwaysNormalized()

取得值,指出目前的編碼方式是否永遠都是使用預設的正規化表單進行正規化。Gets a value indicating whether the current encoding is always normalized, using the default normalization form.

(繼承來源 Encoding)
IsAlwaysNormalized(NormalizationForm)

在衍生類別中覆寫時取得值,指出目前的編碼方式是否永遠都是使用指定的正規化表單進行正規化。When overridden in a derived class, gets a value indicating whether the current encoding is always normalized, using the specified normalization form.

(繼承來源 Encoding)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

擴充方法

GetBytes(Encoding, ReadOnlySequence<Char>)
GetBytes(Encoding, ReadOnlySequence<Char>, IBufferWriter<Byte>)
GetBytes(Encoding, ReadOnlySequence<Char>, Span<Byte>)
GetBytes(Encoding, ReadOnlySpan<Char>, IBufferWriter<Byte>)
GetChars(Encoding, ReadOnlySequence<Byte>, IBufferWriter<Char>)
GetChars(Encoding, ReadOnlySequence<Byte>, Span<Char>)
GetChars(Encoding, ReadOnlySpan<Byte>, IBufferWriter<Char>)
GetString(Encoding, ReadOnlySequence<Byte>)

適用於

另請參閱