Partager via


Convert.ToBase64CharArray Method

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

Converts a subset of an 8-bit unsigned integer array to an equivalent subset of a Unicode character array encoded with base 64 digits. Parameters specify the subsets as offsets in the input and output arrays, and the number of elements in the input array to convert.

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

Syntax

'Declaration
Public Shared Function ToBase64CharArray ( _
    inArray As Byte(), _
    offsetIn As Integer, _
    length As Integer, _
    outArray As Char(), _
    offsetOut As Integer _
) As Integer
public static int ToBase64CharArray(
    byte[] inArray,
    int offsetIn,
    int length,
    char[] outArray,
    int offsetOut
)

Parameters

  • inArray
    Type: array<System.Byte[]
    An input array of 8-bit unsigned integers.
  • length
    Type: System.Int32
    The number of elements of inArray to convert.
  • outArray
    Type: array<System.Char[]
    An output array of Unicode characters.

Return Value

Type: System.Int32
A 32-bit signed integer containing the number of bytes in outArray.

Exceptions

Exception Condition
ArgumentNullException

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

ArgumentOutOfRangeException

offsetIn, offsetOut, or length is negative.

-or-

offsetIn plus length is greater than the length of inArray.

-or-

offsetOut plus the number of elements to return is greater than the length of outArray.

Remarks

The subset of length elements of inArray starting at position offsetIn, are taken as a numeric value and converted to a subset of elements in outArray starting at position offsetOut. The return value indicates the number of converted elements in outArray. The subset of outArray consists of 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.

The offset and length parameters are 32-bit signed numbers. The offsetIn and offsetOut parameters are zero-based array positions.

Examples

The following example demonstrates using the ToBase64CharArray method to convert a string to a UUEncoded character array and then convert it 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 Char array.
Dim encodedChars(1024) As Char 
Dim nChars As Integer      ' Length of character array.
nChars = Convert.ToBase64CharArray(bytes, 0, bytes.Length, _
                                   encodedChars, 0)
' Display the encoded characters.
Dim encodedString As New String(encodedChars) 
outputBlock.Text &= String.Format("The UUEncoded string: {0}   {1}", _
                    vbCrLf, encodedString.Substring(0, nChars)) & vbCrLf

' Convert UUEncoded Char array to a byte array.
bytes = Convert.FromBase64CharArray(encodedChars, 0, _
                                    nChars)

' 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 Char array.
char[] encodedChars = new char[1023]; 
int nChars;                        // Length of character array.
nChars = Convert.ToBase64CharArray(bytes, 0, bytes.Length, 
                                   encodedChars, 0);
// Display the encoded characters.
string encodedString = new string(encodedChars); 
outputBlock.Text += String.Format("The UUEncoded string:\n   {0}\n", 
                                  encodedString.Substring(0, nChars)); 

// Convert UUEncoded Char array to a byte array.
bytes = Convert.FromBase64CharArray(encodedChars, 0, nChars);

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