Convert.FromBase64String Method (String)
Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- s
-
Type:
System.String
The string to convert.
| Exception | Condition |
|---|---|
| ArgumentNullException | s is null. |
| FormatException | The length of s, ignoring white-space characters, is not zero or a multiple of 4. -or- The format of s is invalid. s contains a non-base-64 character, more than two padding characters, or a non-white space-character among the padding characters. |
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, and their Unicode names and hexadecimal code points, are tab (CHARACTER TABULATION, U+0009), newline (LINE FEED, U+000A), carriage return (CARRIAGE RETURN, U+000D), and blank (SPACE, U+0020). 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.
Important |
|---|
The FromBase64Stringmethod is designed to process a single string that contains all the data to be decoded. To decode base-64 character data from a stream, use the System.Security.Cryptography.FromBase64Transform class. |
The following example uses the ToBase64String(Byte()) method to convert a byte array to a UUencoded (base-64) string, and then calls the FromBase64String(String) method to restore the original byte array.
Module Example Public Sub Main() ' Define a byte array. Dim bytes() As Byte = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 } Console.WriteLine("The byte array: ") Console.WriteLine(" {0}", BitConverter.ToString(bytes)) Console.WriteLine() ' Convert the array to a base 64 sring. Dim s As String = Convert.ToBase64String(bytes) Console.WriteLine("The base 64 string:{1} {0}{1}", s, vbCrLf) ' Restore the byte array. Dim newBytes() As Byte = Convert.FromBase64String(s) Console.WriteLine("The restored byte array: ") Console.WriteLine(" {0}", BitConverter.ToString(newBytes)) Console.WriteLine() End Sub End Module ' The example displays the following output: ' The byte array: ' 02-04-06-08-0A-0C-0E-10-12-14 ' ' The base 64 string: ' AgQGCAoMDhASFA== ' ' The restored byte array: ' 02-04-06-08-0A-0C-0E-10-12-14
The following is a more complex example that creates a 20-element array of 32-bit integers. It then uses the BitConverter.GetBytes(Int32) method to convert each element into a byte array, which it stores in the appropriate position in a buffer by calling the Array.Copy(Array, Int32, Array, Int32, Int32) method. This buffer is then passed to the ToBase64String(Byte()) method to create a UUencoded (base-64) string. It then calls the FromBase64String(String) method to decode the UUencoded string, and calls the BitConverter.ToInt32 method to convert each set of four bytes (the size of a 32-bit integer) to an integer. The output from the example shows that the original array has been successfully restored.
Module Example Public Sub Main() ' Define an array of 20 elements and display it. Dim arr(19) As Integer Dim value As Integer = 1 For ctr As Integer = 0 To arr.GetUpperBound(0) arr(ctr) = value value = value * 2 + 1 Next DisplayArray(arr) ' Convert the array of integers to a byte array. Dim bytes(arr.Length * 4 - 1) As Byte For ctr As Integer = 0 To arr.Length - 1 Array.Copy(BitConverter.GetBytes(arr(ctr)), 0, bytes, ctr * 4, 4) Next ' Encode the byte array using Base64 encoding Dim base64 As String = Convert.ToBase64String(bytes) Console.WriteLine("The encoded string: ") For ctr As Integer = 0 To base64.Length \ 50 - 1 Console.WriteLine(base64.Substring(ctr * 50, If(ctr * 50 + 50 <= base64.Length, 50, base64.Length - ctr * 50))) Next Console.WriteLine() ' Convert the string back to a byte array. Dim newBytes() As Byte = Convert.FromBase64String(base64) ' Convert the byte array back to an integer array. Dim newArr(newBytes.Length\4 - 1) As Integer For ctr As Integer = 0 To newBytes.Length \ 4 - 1 newArr(ctr) = BitConverter.ToInt32(newBytes, ctr * 4) Next DisplayArray(newArr) End Sub Private Sub DisplayArray(arr As Array) Console.WriteLine("The array:") Console.Write("{ ") For ctr As Integer = 0 To arr.GetUpperBound(0) - 1 Console.Write("{0}, ", arr.GetValue(ctr)) If (ctr + 1) Mod 10 = 0 Then Console.Write("{0} ", vbCrLf) Next Console.WriteLine("{0} {1}", arr.GetValue(arr.GetUpperBound(0)), "}") Console.WriteLine() End Sub End Module ' The example displays the following output: ' The array: ' { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, ' 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 } ' ' The encoded string: ' AQAAAAMAAAAHAAAADwAAAB8AAAA/AAAAfwAAAP8AAAD/AQAA/w ' MAAP8HAAD/DwAA/x8AAP8/AAD/fwAA//8AAP//AQD//wMA//8H ' ' The array: ' { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, ' 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
