Base64FormattingOptions Enumeration
Specifies whether relevant Convert.ToBase64CharArray and Convert.ToBase64String methods insert line breaks in their output.
This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.
Assembly: mscorlib (in mscorlib.dll)
| Member name | Description | |
|---|---|---|
| InsertLineBreaks | Inserts line breaks after every 76 characters in the string representation. | |
| None | Does not insert line breaks after every 76 characters in the string representation. |
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. 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). Because line breaks are considered whitespace 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.
The Base64FormattingOptions.None and Base64FormattingOptions.InsertLineBreaks values are mutually exclusive. Therefore, although the Base64FormattingOptions enumeration is marked with the FlagsAttribute attribute, it makes no sense to perform a bitwise combination of these two values.
The following example calls the Convert.ToBase64String(Byte[], Base64FormattingOptions) with a Base64FormattingOptions.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. Byte[] 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 sring. 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
Available since 2.0
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.