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자마다 줄 바꿈을 삽입하지 않습니다.

예제

다음 예제에서는 인수를 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 주듯이 원래 바이트 배열을 복원하는 데 성공합니다. 변환 중에 줄 바꿈 문자는 무시됩니다.

설명

Convert.ToBase64String 메서드는 Convert.ToBase64CharArray 부호 없는 8비트 정수 배열의 값을 기본 64자리 숫자로 구성된 동등한 문자열 표현으로 변환합니다. 문자열 표현은 줄 바꿈을 하나 이상 포함할 수 있습니다. 여기서 줄 바꿈은 캐리지 리턴 문자(U+000D) 뒤에 줄 바꿈 문자(U+000A)로 정의됩니다. 줄 바꿈은 base-64 인코딩의 공백 문자로 간주되므로 base-64로 인코딩된 문자열을 바이트 배열로 다시 변환할 때 무시됩니다. 줄 바꿈은 단순히 편리한 경우가 컨트롤 또는 콘솔 창과 같은 디바이스에 인코딩된 문자열을 표시 합니다.

값과 InsertLineBreaks 값은 None 함께 사용할 수 없습니다. 따라서 열거형이 특성으로 FlagsAttribute 표시되어 있지만 Base64FormattingOptions 이러한 두 값의 비트 조합을 수행하는 것은 의미가 없습니다.

적용 대상