This documentation is archived and is not being maintained.

Convert.ToBase64String Method (Byte[], Base64FormattingOptions)

Updated: June 2011

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 function ToBase64String(
	inArray : byte[], 
	options : Base64FormattingOptions
) : String

Parameters

inArray
Type: System.Byte[]

An array of 8-bit unsigned integers.

options
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.

ExceptionCondition
ArgumentNullException

inArray is a null reference (Nothing in Visual Basic).

ArgumentException

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.

Important noteImportant Note:

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). For more information, see RFC 2045, "Multipurpose Internet Mail Extensions", at http://www.rfc-editor.org/.

The following example demonstrates the ToBase64String method. The input is divided into groups of three bytes (24 bits) each. Consequently, each group consists of four 6-bit numbers where each number ranges from decimal 0 to 63. In this example, there are 85 3-byte groups with one byte remaining. The first group consists of the hexadecimal values 00, 01, and 02, which yield four 6-bit values equal to decimal 0, 0, 4, and 2. Those four values correspond to the base-64 digits "A", "A", "E", and "C" at the beginning of the output.

If an integral number of 3-byte groups does not exist, the remaining bytes are effectively padded with zeros to form a complete group. In this example, the value of the last byte is hexadecimal FF. The first 6 bits are equal to decimal 63, which corresponds to the base-64 digit "/" at the end of the output, and the next 2 bits are padded with zeros to yield decimal 48, which corresponds to the base-64 digit, "w". The last two 6-bit values are padding and correspond to the valueless padding character, "=".

No code example is currently available or this language may not be supported.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Date

History

Reason

June 2011

Added a note about converting data in a stream.

Customer feedback.

Show: