asp.net.ph

Convert.FromBase64String Method

System Namespace   Convert Class


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

[ VB ]
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 is less than 4.

-or-

The length of s is not an even multiple of 4.


Remarks

s is composed of base 64 digits. 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 valueless character, '=', is used for trailing padding.

Example

The following sample demonstrates using the FromBase64String method.

[ VB ]
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.Response.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.Response.WriteLine ( "Base 64 string is null." )
      Return
   Catch exp As System.FormatException
      System.Response.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.Response.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.Response.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.Response.WriteLine ( "Base 64 string is null." );
            return;
         }
         catch ( System.FormatException ) {
            System.Response.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.Response.WriteLine ( "{0}", exp.Message );
         }
      }
See Also

Convert Members Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph