Convert.ToBase64String() Method in C#

The Convert.ToBase64String() method in C# is used to convert the value of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits. This is commonly used for encoding binary data into a text format that can be safely transmitted or stored.

Syntax

Following is the syntax −

public static string ToBase64String(byte[] inArray);

Parameters

  • inArray − An array of 8-bit unsigned integers to be converted to Base64 string.

Return Value

Returns a string representation of the contents of the byte array encoded in Base64.

Example

Let us now see an example to implement the Convert.ToBase64String() method −

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 ToBase64String() with Text

A common use case is encoding text data to Base64 format −

using System;
using System.Text;

public class TextToBase64 {
   public static void Main() {
      string originalText = "Hello World!";
      byte[] textBytes = Encoding.UTF8.GetBytes(originalText);
      string base64String = Convert.ToBase64String(textBytes);
      
      Console.WriteLine("Original text: {0}", originalText);
      Console.WriteLine("Base64 encoded: {0}", base64String);
      
      // Decode back to verify
      byte[] decodedBytes = Convert.FromBase64String(base64String);
      string decodedText = Encoding.UTF8.GetString(decodedBytes);
      Console.WriteLine("Decoded text: {0}", decodedText);
   }
}

The output of the above code is −

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

Using ToBase64String() with Different Data Types

You can also convert other data types by first converting them to byte arrays −

using System;

public class DataTypeEncoding {
   public static void Main() {
      // Integer to Base64
      int number = 12345;
      byte[] intBytes = BitConverter.GetBytes(number);
      string intBase64 = Convert.ToBase64String(intBytes);
      Console.WriteLine("Integer {0} as Base64: {1}", number, intBase64);
      
      // Double to Base64
      double decimalValue = 123.456;
      byte[] doubleBytes = BitConverter.GetBytes(decimalValue);
      string doubleBase64 = Convert.ToBase64String(doubleBytes);
      Console.WriteLine("Double {0} as Base64: {1}", decimalValue, doubleBase64);
      
      // Boolean to Base64
      bool boolValue = true;
      byte[] boolBytes = BitConverter.GetBytes(boolValue);
      string boolBase64 = Convert.ToBase64String(boolBytes);
      Console.WriteLine("Boolean {0} as Base64: {1}", boolValue, boolBase64);
   }
}

The output of the above code is −

Integer 12345 as Base64: OTA=
Double 123.456 as Base64: F9nPVDHedkA=
Boolean True as Base64: AQ==

Common Use Cases

  • Data transmission − Encoding binary data for safe transmission over text-based protocols like HTTP or email.

  • Configuration storage − Storing binary configuration data in XML or JSON files.

  • Image encoding − Embedding images in HTML or CSS using data URLs.

  • Cryptographic operations − Encoding encrypted data or cryptographic keys for storage or transmission.

Conclusion

The Convert.ToBase64String() method is essential for encoding binary data into a Base64 string format. It's particularly useful when you need to transmit or store binary data in environments that only support text, ensuring data integrity and compatibility across different systems.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements