Convert.ToBase64String Method (Byte[], Base64FormattingOptions)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits. A parameter specifies whether to insert line breaks in the return value.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public static string ToBase64String(
	byte[] inArray,
	Base64FormattingOptions options


Type: System.Byte[]

An array of 8-bit unsigned integers.

Type: System.Base64FormattingOptions

InsertLineBreaks to insert a line break every 76 characters, or None to not insert line breaks.

Return Value

Type: System.String

The string representation in base 64 of the elements in inArray.

Exception Condition

inArray is null.


options is not a valid Base64FormattingOptions value.

The elements of the inArray parameter are taken as a numeric value and converted to a string representation in base 64.

The base-64 digits in ascending order from zero are the uppercase characters "A" to "Z", the lowercase characters "a" to "z", the numerals "0" to "9", and the symbols "+" and "/". The valueless character "=" is used for trailing padding.


The ToBase64String method is designed to process a single byte array that contains all the data to be encoded. To encode data from a stream, use the System.Security.Cryptography.ToBase64Transform class.

If the options parameter is set to InsertLineBreaks and the output of the conversion is longer than 76 characters, a line break is inserted every 76 characters. 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. For more information, see RFC 2045, "Multipurpose Internet Mail Extensions", at

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}",
       Console.WriteLine("   Sum of elements: {0:N0}", originalTotal);                  

       // Convert the array to a base 64 sring.
       String s = Convert.ToBase64String(bytes, 
       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}",
       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:
//      Total elements: 100
//      Length of String Representation: 299
//      Sum of elements: 5,050

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.

.NET Framework
Available since 2.0
Return to top