FromBase64String Method

Convert.FromBase64String Method

Converts the specified String representation of a value consisting of base 64 digits to an equivalent array of 8-bit unsigned integers.

[Visual Basic]
Public Shared Function FromBase64String( _
   ByVal s As String _
) As Byte()
[C#]
public static byte[] FromBase64String(
 string s
);
[C++]
public: static unsigned char FromBase64String(
 String* s
)  __gc[];
[JScript]
public static function FromBase64String(
   s : String
) : Byte[];

Parameters

s
A String.

Return Value

An array of 8-bit unsigned integers equivalent to s.

Exceptions

Exception Type Condition
ArgumentNullException s is a null reference (Nothing in Visual Basic).
FormatException The length of s, ignoring white space characters, is less than 4.

-or-

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

Remarks

s 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 s because all white space characters are ignored.

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

Example

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

[Visual Basic] 
Public Sub DecodeWithString()
   Dim inFile As System.IO.StreamReader
   Dim base64String As String

   Try
      Dim base64CharArray() As Char
      inFile = New System.IO.StreamReader(inputFileName, _
                                          System.Text.Encoding.ASCII)
      base64CharArray = New Char(inFile.BaseStream.Length) {}
      inFile.Read(base64CharArray, 0, inFile.BaseStream.Length)
      base64String = New String(base64CharArray, _
                                0, _
                                base64CharArray.Length - 1)
   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.FromBase64String(base64String)
   Catch exp As System.ArgumentNullException
      System.Console.WriteLine("Base 64 string is null.")
      Return
   Catch exp As System.FormatException
      System.Console.WriteLine("Base 64 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 DecodeWithString() {
    System.IO.StreamReader inFile;     
    string base64String;

    try {
        char[] base64CharArray;
        inFile = new System.IO.StreamReader(inputFileName,
                                        System.Text.Encoding.ASCII);
        base64CharArray = new char[inFile.BaseStream.Length];
        inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
        base64String = new string(base64CharArray);
    }
    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.FromBase64String(base64String);
    }
    catch (System.ArgumentNullException) {
        System.Console.WriteLine("Base 64 string is null.");
        return;
    }
    catch (System.FormatException) {
        System.Console.WriteLine("Base 64 string 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 DecodeWithString() {
      StreamReader* inFile;     
      String* base64String;

      try {
         Char base64CharArray[];
         inFile = new StreamReader(inputFileName,
            Text::Encoding::ASCII);
         base64CharArray = new Char[(int)(inFile->BaseStream->Length)];
         inFile->Read(base64CharArray, 0, (int)inFile->BaseStream->Length);
         base64String = new String(base64CharArray);
      } 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::FromBase64String(base64String);
      } catch (ArgumentNullException*) {
         Console::WriteLine(S"Base 64 String* is null.");
         return;
      } catch (FormatException*) {
         Console::WriteLine(S"Base 64 String* 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

Show:
© 2016 Microsoft