Base64FormattingOptions 列挙型

定義

関連する ToBase64CharArray メソッドと ToBase64String メソッドで、それぞれの出力に改行を挿入するかどうかを指定します。

この列挙体は、メンバー値のビットごとの組み合わせをサポートしています。

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

フィールド

InsertLineBreaks 1

文字列形式で 76 文字ごとに改行を挿入します。

None 0

文字列形式で 76 文字ごとに改行を挿入しません。

次の例では、引数を指定してメソッドをConvert.ToBase64String(Byte[], Base64FormattingOptions)InsertLineBreaks呼び出して、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 バイト配列の復元に成功します。変換中は改行文字は無視されます。

注釈

and Convert.ToBase64String メソッドはConvert.ToBase64CharArray、8 ビット符号なし整数の配列の値を、基本の 64 桁で構成される同等の文字列表現に変換します。 文字列表現には 1 つ以上の改行を含めることができます。改行は復帰文字 (U+000D) の後に改行文字 (U+000A) が続くものとして定義されます。 改行は base-64 エンコードでは空白文字と見なされるため、base-64 でエンコードされた文字列をバイト配列に変換する場合は無視されます。 改行は、エンコードされた文字列をコントロールやコンソール ウィンドウなどのデバイスに表示する場合に便利です。

NoneInsertLineBreaks 値は相互に排他的です。 したがって、列挙体は属性でFlagsAttributeマークされていますがBase64FormattingOptions、これら 2 つの値のビットごとの組み合わせを実行しても意味がありません。

適用対象