Last Updated : 11 Jul, 2025
This method is used to convert the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array.
Syntax:public static byte[] FromBase64String (string s);
Here,
sis the string to convert.
Return Value:This method returns an array of 8-bit unsigned integers that is equivalent to
s.
ExceptionsBelow programs illustrate the use of
Convert.FromBase64String(String)Method:
Example 1: csharp
// C# program to demonstrate the
// Convert.FromBase64String(String) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// defining and initializing
// byte1 and byte2
byte[] byte1 = {2, 4, 6, 8, 10,
12, 14, 16, 18, 20};
byte[] byte2 = {10, 20, 30,
40, 50};
// calling get() Method
get(byte1, "byte1");
Console.WriteLine("");
get(byte2, "byte2");
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (FormatException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(),
e.Message);
}
}
// Defining get() method
public static void get(byte[] bytes, string str)
{
Console.WriteLine("For {0}", str);
// converting byte to base 64 string
string s = Convert.ToBase64String(bytes);
Console.WriteLine("The base 64 string: '{0}'", s);
Console.WriteLine("The base 64 string: '{0}'", s);
// converting base 64 string to byte array
byte[] val = Convert.FromBase64String(s);
Console.WriteLine("Converted byte value: {0}",
BitConverter.ToString(val));
}
}
Output:
For byte1 The base 64 string: 'AgQGCAoMDhASFA==' The base 64 string: 'AgQGCAoMDhASFA==' Converted byte value: 02-04-06-08-0A-0C-0E-10-12-14 For byte2 The base 64 string: 'ChQeKDI=' The base 64 string: 'ChQeKDI=' Converted byte value: 0A-14-1E-28-32Example 2:
For
ArgumentNullException csharp
// C# program to demonstrate the
// Convert.FromBase64String(String) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// defining and initializing
// byte1 and byte2
byte[] byte1 = {2, 4, 6, 8, 10, 12,
14, 16, 18, 20};
// calling get() Method
get(byte1, "byte1");
Console.WriteLine("");
// converting base 64 string to byte array
Console.WriteLine("s is null");
byte[] val = Convert.FromBase64String(null);
Console.WriteLine("Converted byte value: {0}",
BitConverter.ToString(val));
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (FormatException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(byte[] bytes, string str)
{
Console.WriteLine("For {0}", str);
// converting byte to base 64 string
string s = Convert.ToBase64String(bytes);
Console.WriteLine("The base 64 string: '{0}'", s);
// converting base 64 string to byte array
byte[] val = Convert.FromBase64String(s);
Console.WriteLine("Converted byte value: {0}",
BitConverter.ToString(val));
}
}
Output:
For byte1 The base 64 string: 'AgQGCAoMDhASFA==' Converted byte value: 02-04-06-08-0A-0C-0E-10-12-14 s is null Exception Thrown: System.ArgumentNullExceptionExample 3:
For
FormatException csharp
// C# program to demonstrate the
// Convert.FromBase64String(String) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// defining and initializing
// byte1 and byte2
byte[] byte1 = {2, 4, 6, 8, 10,
12, 14, 16, 18, 20};
// calling get() Method
get(byte1, "byte1");
Console.WriteLine("");
// converting base 64
// string to byte array
Console.WriteLine("s contains a non-base-64 character");
byte[] val = Convert.FromBase64String("sun");
Console.WriteLine("Converted byte value: {0}",
BitConverter.ToString(val));
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (FormatException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(byte[] bytes, string str)
{
Console.WriteLine("For {0}", str);
// converting byte to base 64 string
string s = Convert.ToBase64String(bytes);
Console.WriteLine("The base 64 string: '{0}'", s);
// converting base 64 string to byte array
byte[] val = Convert.FromBase64String(s);
Console.WriteLine("Converted byte value: {0}",
BitConverter.ToString(val));
}
}
Output:
For byte1 The base 64 string: 'AgQGCAoMDhASFA==' Converted byte value: 02-04-06-08-0A-0C-0E-10-12-14 s contains a non-base-64 character Exception Thrown: System.FormatExceptionReference:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4