閱讀英文版本

分享方式:


Base64FormattingOptions 列舉

定義

指定相關 ToBase64CharArrayToBase64String 方法是否在其輸出中插入分行符號。

此列舉支援其成員值的位元組合。

C#
[System.Flags]
public enum Base64FormattingOptions
繼承
Base64FormattingOptions
屬性

欄位

名稱 Description
InsertLineBreaks 1

在字串表示的每 76 個字元後面插入分行符號。

None 0

請不要在字串表示的每 76 個字元後面插入分行符號。

範例

下列範例會使用 InsertLineBreaks 引數呼叫 Convert.ToBase64String(Byte[], Base64FormattingOptions) 方法,以在編碼 100 個元素位元組陣列所產生的字串中插入分行符號:

C#
using System;

public class Example
{
   public static void Main()
   {
       // Define a byte array.
       var bytes = new byte[100];
       int originalTotal = 0;
       for (int ctr = 0; ctr <= bytes.GetUpperBound(0); ctr++) {
          bytes[ctr] = (byte)(ctr + 1);
          originalTotal += bytes[ctr];
       }
       // Display summary information about the array.
       Console.WriteLine("The original byte array:");
       Console.WriteLine("   Total elements: {0}", bytes.Length);
       Console.WriteLine("   Length of String Representation: {0}",
                         BitConverter.ToString(bytes).Length);
       Console.WriteLine("   Sum of elements: {0:N0}", originalTotal);
       Console.WriteLine();

       // Convert the array to a base 64 string.
       string s = Convert.ToBase64String(bytes,
                                         Base64FormattingOptions.InsertLineBreaks);
       Console.WriteLine("The base 64 string:\n   {0}\n", s);

       // Restore the byte array.
       Byte[] newBytes = Convert.FromBase64String(s);
       int newTotal = 0;
       foreach (var newByte in newBytes)
          newTotal += newByte;

       // Display summary information about the restored array.
       Console.WriteLine("   Total elements: {0}", newBytes.Length);
       Console.WriteLine("   Length of String Representation: {0}",
                         BitConverter.ToString(newBytes).Length);
       Console.WriteLine("   Sum of elements: {0:N0}", newTotal);
   }
}
// The example displays the following output:
//   The original byte array:
//      Total elements: 100
//      Length of String Representation: 299
//      Sum of elements: 5,050
//
//   The base 64 string:
//      AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5
//   Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZA==
//
//      Total elements: 100
//      Length of String Representation: 299
//      Sum of elements: 5,050

如範例的輸出所示,還原 Convert.FromBase64String 原始位元組陣列時會成功;轉換期間會忽略分行符號。

備註

Convert.ToBase64CharArrayConvert.ToBase64String 方法會將 8 位不帶正負號整數陣列的值,轉換為由基底 64 位陣列成的對等字串表示。 字串表示可以包含一或多個分行符號,其中分行符號定義為歸位字元, (U+000D) 後面接著換行字元, (U+000A) 。 由於分行符號在 base-64 編碼中被視為空白字元,因此在將 base-64 編碼字串轉換回位元組陣列時會忽略這些字元。 將編碼字串顯示至控制項或主控台視窗之類的裝置時,分行符號只是方便的。

NoneInsertLineBreaks 值互斥。 因此,雖然 Base64FormattingOptions 列舉是以 FlagsAttribute 屬性標示,但執行這兩個值的位元組合併不合理。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1