Share via


Convert.ToBase64String Method (array<Byte[])

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts an array of 8-bit unsigned integers to its equivalent String representation encoded with base 64 digits.

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

Syntax

'Declaration
Public Shared Function ToBase64String ( _
    inArray As Byte() _
) As String
public static string ToBase64String(
    byte[] inArray
)

Parameters

  • inArray
    Type: array<System.Byte[]
    An array of 8-bit unsigned integers.

Return Value

Type: System.String
The String representation, in base 64, of the contents of inArray.

Exceptions

Exception Condition
ArgumentNullException

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

Remarks

The elements of inArray are taken as a numeric value and converted to a String representation encoded with base 64 digits.

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.

Examples

The following example uses the ToBase64String(array<Byte[]) method to convert an array of UTF16-encoded bytes to a UUEncoded string. It then uses the FromBase64String method to convert the UUEncoded string back to the original string.

Dim bytes() As Byte
Dim originalString As String = "The contents of the original string form a sentence."
outputBlock.Text &= String.Format("The original string: {0}   {1}", _
                    vbCrLf, originalString) & vbCrLf

' Convert the string to a byte array.
Dim encoder As New UnicodeEncoding()
bytes = encoder.GetBytes(originalString)

' Convert the byte array to a base 64 encoded string.
Dim encodedString As String = Convert.ToBase64String(bytes)
' Display the encoded string.
outputBlock.Text &= String.Format("The UUEncoded string: {0}   {1}", _
                    vbCrLf, encodedString) & vbCrLf

' Convert UUEncoded string to a byte array.
bytes = Convert.FromBase64String(encodedString)

' Convert byte array back to the original string.
originalString = encoder.GetString(bytes, 0, bytes.Length) 
outputBlock.Text &= String.Format("The original string restored: {0}   {1}", _
                    vbCrLf, originalString) & vbCrLf
' The example displays the following output:
'       The original string:
'          The contents of the original string form a sentence.
'       The UUEncoded string:
'          VABoAGUAIABjAG8AbgB0AGUAbgB0AHMAIABvAGYAIAB0AGgAZQAgAG8AcgBpAGcAaQBuAGEAbAAgA
'       HMAdAByAGkAbgBnACAAZgBvAHIAbQAgAGEAIABzAGUAbgB0AGUAbgBjAGUALgA=
'       The original string restored:
'          The contents of the original string form a sentence.
byte[] bytes;
string originalString = "The contents of the original string form a sentence.";
outputBlock.Text += String.Format("The original string:\n   {0}\n", 
                                  originalString);

// Convert the string to a byte array.
UnicodeEncoding encoder = new UnicodeEncoding();
bytes = encoder.GetBytes(originalString);

// Convert the byte array to a base 64 encoded string.
string encodedString = Convert.ToBase64String(bytes);
// Display the encoded string.
outputBlock.Text += String.Format("The UUEncoded string:\n   {0}\n", 
                                  encodedString);

// Convert UUEncoded string to a byte array.
bytes = Convert.FromBase64String(encodedString);

// Convert byte array back to the original string.
originalString = encoder.GetString(bytes, 0, bytes.Length); 
outputBlock.Text += String.Format("The original string restored:\n   {0}\n", 
                                  originalString);
// The example displays the following output:
//       The original string:
//          The contents of the original string form a sentence.
//       The UUEncoded string:
//          VABoAGUAIABjAG8AbgB0AGUAbgB0AHMAIABvAGYAIAB0AGgAZQAgAG8AcgBpAGcAaQBuAGEAbAAgA
//       HMAdAByAGkAbgBnACAAZgBvAHIAbQAgAGEAIABzAGUAbgB0AGUAbgBjAGUALgA=
//       The original string restored:
//          The contents of the original string form a sentence.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.