Convert.FromBase64String(String) Method in C#

The Convert.FromBase64String(String) method in C# converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array. This method is commonly used for decoding base64-encoded data back to its original byte representation.

Syntax

Following is the syntax −

public static byte[] FromBase64String (string str);

Parameters

str − The string to convert. It must be a valid base64-encoded string.

Return Value

Returns a byte[] array containing the decoded binary data from the base64 string.

Using FromBase64String for Byte Array Conversion

The most common use case is converting a base64 string back to its original byte array −

using System;

public class Demo {
   public static void Main() {
      byte[] val1 = {5, 10, 15, 20, 25, 30};
      string str = Convert.ToBase64String(val1);
      Console.WriteLine("Base 64 string: '{0}'", str);
      
      byte[] val2 = Convert.FromBase64String(str);
      Console.WriteLine("Converted byte value: {0}", BitConverter.ToString(val2));
   }
}

The output of the above code is −

Base 64 string: 'BQoPFBke'
Converted byte value: 05-0A-0F-14-19-1E

Using FromBase64String for Text Decoding

You can also use this method to decode base64-encoded text data −

using System;
using System.Text;

public class TextDecodeDemo {
   public static void Main() {
      string originalText = "Hello World!";
      string base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(originalText));
      Console.WriteLine("Original text: " + originalText);
      Console.WriteLine("Base64 encoded: " + base64String);
      
      byte[] decodedBytes = Convert.FromBase64String(base64String);
      string decodedText = Encoding.UTF8.GetString(decodedBytes);
      Console.WriteLine("Decoded text: " + decodedText);
   }
}

The output of the above code is −

Original text: Hello World!
Base64 encoded: SGVsbG8gV29ybGQh
Decoded text: Hello World!

Exception Handling

The method throws a FormatException if the input string is not a valid base64 string −

using System;

public class ExceptionDemo {
   public static void Main() {
      try {
         string invalidBase64 = "InvalidBase64String!!!";
         byte[] result = Convert.FromBase64String(invalidBase64);
         Console.WriteLine("Conversion successful");
      }
      catch (FormatException ex) {
         Console.WriteLine("FormatException: " + ex.Message);
      }
      
      try {
         string validBase64 = "SGVsbG8=";
         byte[] result = Convert.FromBase64String(validBase64);
         Console.WriteLine("Valid conversion: " + System.Text.Encoding.UTF8.GetString(result));
      }
      catch (FormatException ex) {
         Console.WriteLine("Error: " + ex.Message);
      }
   }
}

The output of the above code is −

FormatException: Invalid length for a Base-64 char array or string.
Valid conversion: Hello

Conclusion

The Convert.FromBase64String() method is essential for decoding base64-encoded data back to byte arrays. It's commonly used in data transmission, file storage, and web applications where binary data needs to be represented as text and then converted back to its original form.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements