共用方式為


Base64FormattingOptions 列舉

定義

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

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

public enum class Base64FormattingOptions
[System.Flags]
public enum Base64FormattingOptions
[<System.Flags>]
type Base64FormattingOptions = 
Public Enum Base64FormattingOptions
繼承
Base64FormattingOptions
屬性

欄位

InsertLineBreaks 1

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

None 0

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

範例

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

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
open System

// Define a byte array.
let bytes = 
    [| for i = 0 to 99 do byte (i + 1) |]

let originalTotal = Array.sumBy int bytes

// Display summary information about the array.
printfn "The original byte array:"
printfn $"   Total elements: {bytes.Length}"
printfn $"   Length of String Representation: {BitConverter.ToString(bytes).Length}"
printfn $"   Sum of elements: {originalTotal:N0}"
printfn ""

// Convert the array to a base 64 string.
let s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks)
printfn $"The base 64 string:\n   {s}\n"

// Restore the byte array.
let newBytes = Convert.FromBase64String s

let newTotal = Array.sumBy int newBytes

// Display summary information about the restored array.
printfn $"   Total elements: {newBytes.Length}"
printfn $"   Length of String Representation: {BitConverter.ToString(newBytes).Length}"
printfn $"   Sum of elements: {newTotal:N0}"

// 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
Module Example
   Public Sub Main()
       ' Define a byte array.
       Dim bytes(99) As Byte
       Dim originalTotal As Integer = 0
       For ctr As Integer = 0 To bytes.GetUpperBound(0)
          bytes(ctr) = CByte(ctr + 1)
          originalTotal += bytes(ctr)
       Next
       ' 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.
       Dim s As String = Convert.ToBase64String(bytes, 
                                               Base64FormattingOptions.InsertLineBreaks)
       Console.WriteLine("The base 64 string:{1}   {0}{1}", 
                         s, vbCrLf)
       
       ' Restore the byte array.
       Dim newBytes() As Byte = Convert.FromBase64String(s)
       Dim newTotal As Integer = 0
       For Each newByte In newBytes
          newTotal += newByte
       Next
       ' 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)                  
   End Sub
End Module
' 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 屬性標示,但執行這兩個值的位元組合併不合理。

適用於