Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
This topic has not yet been rated - Rate this topic

Convert.FromBase64CharArray Method

Converts the specified subset of an array of Unicode characters consisting of base 64 digits to an equivalent array of 8-bit unsigned integers. Parameters specify the subset as an offset in the input array and the number of elements in the array to convert.

[Visual Basic]
Public Shared Function FromBase64CharArray( _
   ByVal inArray() As Char, _
   ByVal offset As Integer, _
   ByVal length As Integer _
) As Byte()
[C#]
public static byte[] FromBase64CharArray(
 char[] inArray,
 int offset,
 int length
);
[C++]
public: static unsigned char FromBase64CharArray(
 __wchar_t inArray __gc[],
 int offset,
 int length
)  __gc[];
[JScript]
public static function FromBase64CharArray(
   inArray : Char[],
 offset : int,
 length : int
) : Byte[];

Parameters

inArray
A Unicode character array.
offset
A position within inArray.
length
The number of elements in inArray to convert.

Return Value

An array of 8-bit unsigned integers equivalent to length elements at position offset in inArray.

Exceptions

Exception Type Condition
ArgumentNullException inArray is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException offset or length is less than 0.

-or-

offset plus length indicates a position not within inArray.

FormatException The length of inArray, ignoring white space characters, is less than 4.

-or-

The length of inArray, ignoring white space characters, is not an even multiple of 4.

Remarks

inArray is composed of base 64 digits, white space characters, and trailing padding characters. The base 64 digits in ascending order from zero are the uppercase characters 'A' to 'Z', lowercase characters 'a' to 'z', numerals '0' to '9', and the symbols '+' and '/'.

The white space characters are tab, blank, carriage return, and newline. An arbitrary number of white space characters can appear in inArray because all white space characters are ignored.

The valueless character, '=', is used for trailing padding. The end of inArray can consist of zero, one, or two padding characters.

Example

[Visual Basic, C#, C++] The following sample demonstrates the use of the FromBase64CharArray method to decode UUencoded (base 64) data and save it as binary output.

[Visual Basic] 
Public Sub DecodeWithCharArray()
   Dim inFile As System.IO.StreamReader
   Dim base64CharArray() As Char

   Try
      inFile = New System.IO.StreamReader(inputFileName, _
                                          System.Text.Encoding.ASCII)

      ReDim base64CharArray(inFile.BaseStream.Length - 1)
      inFile.Read(base64CharArray, 0, inFile.BaseStream.Length)
      inFile.Close()
   Catch exp As System.Exception
      ' Error creating stream or reading from it.
      System.Console.WriteLine("{0}", exp.Message)
      Return
   End Try

   ' Convert the Base64 UUEncoded input into binary output.
   Dim binaryData() As Byte
   Try
      binaryData = System.Convert.FromBase64CharArray(base64CharArray, 0, _
                                                base64CharArray.Length)
   Catch exp As System.ArgumentNullException
      System.Console.WriteLine("Base 64 character array is null.")
      Return
   Catch exp As System.FormatException
      System.Console.WriteLine("Base 64 Char Array length is not " + _
               "4 or is not an even multiple of 4")
      Return
   End Try

   ' Write out the decoded data.
   Dim outFile As System.IO.FileStream
   Try
      outFile = New System.IO.FileStream(outputFileName, _
                                         System.IO.FileMode.Create, _
                                         System.IO.FileAccess.Write)
      outFile.Write(binaryData, 0, binaryData.Length - 1)
      outFile.Close()
   Catch exp As System.Exception
      ' Error creating stream or writing to it.
      System.Console.WriteLine("{0}", exp.Message)
   End Try
End Sub

[C#] 
public void DecodeWithCharArray() {
    System.IO.StreamReader inFile;     
    char[] base64CharArray;

    try {
        inFile = new System.IO.StreamReader(inputFileName,
                                        System.Text.Encoding.ASCII);
        base64CharArray = new char[inFile.BaseStream.Length];
        inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
        inFile.Close();
    }
    catch (System.Exception exp) {
        // Error creating stream or reading from it.
        System.Console.WriteLine("{0}", exp.Message);
        return;
    }

    // Convert the Base64 UUEncoded input into binary output.
    byte[] binaryData;
    try {
        binaryData = 
            System.Convert.FromBase64CharArray(base64CharArray,
                                               0,
                                               base64CharArray.Length);
    }
    catch ( System.ArgumentNullException ) {
        System.Console.WriteLine("Base 64 character array is null.");
        return;
    }
    catch ( System.FormatException ) {
        System.Console.WriteLine("Base 64 Char Array length is not " +
            "4 or is not an even multiple of 4." );
        return;
    }

    // Write out the decoded data.
    System.IO.FileStream outFile;
    try {
        outFile = new System.IO.FileStream(outputFileName,
                                           System.IO.FileMode.Create,
                                           System.IO.FileAccess.Write);
        outFile.Write(binaryData, 0, binaryData.Length);
        outFile.Close();
    }
    catch (System.Exception exp) {
        // Error creating stream or writing to it.
        System.Console.WriteLine("{0}", exp.Message);
    }
}

[C++] 
public:
   void DecodeWithCharArray() {
      StreamReader* inFile;     
      Char base64CharArray[];

      try {
         inFile = new StreamReader(inputFileName,
            Text::Encoding::ASCII);
         base64CharArray = new Char[(int)(inFile->BaseStream->Length)];
         inFile->Read(base64CharArray, 0, (int)inFile->BaseStream->Length);
         inFile->Close();
      } catch (Exception* exp) {
         // Error creating stream or reading from it.
         Console::WriteLine(S" {0}", exp->Message);
         return;
      }

      // Convert the Base64 UUEncoded input into binary output.
      Byte binaryData[];
      try {
         binaryData = 
            Convert::FromBase64CharArray(base64CharArray,
            0,
            base64CharArray->Length);
      } catch (ArgumentNullException*) {
         Console::WriteLine(S"Base 64 character array is null.");
         return;
      } catch (FormatException*) {
         Console::WriteLine(S"Base 64 Char Array length is not 4 or is not an even multiple of 4.");
         return;
      }

      // Write out the decoded data.
      FileStream* outFile;
      try {
         outFile = new FileStream(outputFileName,
            FileMode::Create,
            FileAccess::Write);
         outFile->Write(binaryData, 0, binaryData->Length);
         outFile->Close();
      } catch (Exception* exp) {
         // Error creating stream or writing to it.
         Console::WriteLine(S" {0}", exp->Message);
      }
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Convert Class | Convert Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.