Base64FormattingOptions 枚举

定义

指定相关 ToBase64CharArrayToBase64String 方法是否在其输出中插入换行符。Specifies whether relevant ToBase64CharArray and ToBase64String methods insert line breaks in their output.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

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

字段

InsertLineBreaks 1

在字符串表示形式中每隔 76 个字符插入换行符。Inserts line breaks after every 76 characters in the string representation.

None 0

不在字符串表示形式中每隔 76 个字符插入换行符。Does not insert line breaks after every 76 characters in the string representation.

示例

下面的示例使用 InsertLineBreaks 参数调用 Convert.ToBase64String(Byte[], Base64FormattingOptions) 方法,以便在通过编码100元素的字节数组生成的字符串中插入换行符:The following example calls the Convert.ToBase64String(Byte[], Base64FormattingOptions) method with a InsertLineBreaks argument to insert line breaks in the string that is produced by encoding a 100-element byte array:

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
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 成功还原原始字节数组;在转换过程中将忽略换行符。As the output from the example shows, the Convert.FromBase64String succeeds in restoring the original byte array; the line break characters are ignored during the conversion.

注解

Convert.ToBase64CharArrayConvert.ToBase64String 方法将8位无符号整数数组的值转换为包含64进制的等效字符串表示形式。The Convert.ToBase64CharArray and Convert.ToBase64String methods convert the value of an array of 8-bit unsigned integers to an equivalent string representation that consists of base 64 digits. 字符串表示形式可以包含一个或多个换行符,其中换行符定义为回车符(U + 000D),后跟换行符(U + 000A)。The string representation can contain one or more line breaks, where a line break is defined as a carriage return character (U+000D) followed by a line feed character (U+000A). 由于分行符在64编码中被视为空白字符,因此在将 base-64 编码的字符串转换回字节数组时,它们将被忽略。Because line breaks are considered white-space characters in a base-64 encoding, they are ignored when converting a base-64 encoded string back to a byte array. 在将编码的字符串显示到控件或设备(如控制台窗口)中时,分行符只是很方便。The line breaks are simply convenient when displaying the encoded string to a control or a device such as a console window.

NoneInsertLineBreaks 值是相互排斥的。The None and InsertLineBreaks values are mutually exclusive. 因此,尽管使用 FlagsAttribute 属性标记 Base64FormattingOptions 枚举,但没有任何意义来执行这两个值的按位组合。Therefore, although the Base64FormattingOptions enumeration is marked with the FlagsAttribute attribute, it makes no sense to perform a bitwise combination of these two values.

适用于