Open In App

C# | Convert.ToBase64String() Method | Set-1

Last Updated : 03 Apr, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
Convert.ToBase64String() Method is used to convert the value of an array of 8-bit unsigned integers to its equivalent string representation which is encoded with base-64 digits. There are 4 methods in the overload of this method which are as follows:
  • ToBase64String(Byte[], Int32, Int32) Method
  • ToBase64String(Byte[], Int32, Int32, Base64FormattingOptions) Method
  • ToBase64String(Byte[], Base64FormattingOptions) Method
  • ToBase64String(Byte[]) Method
Here, we will discuss only the first method. ToBase64String(Byte[], Int32, Int32) Method is used to convert a subset of an array of 8-bit unsigned integers to its equivalent string representation which is encoded with base-64 digits. Parameters specify the subset as an offset in the input array, and the number of elements in the array to convert. Syntax:
public static string ToBase64String (byte[] inArray, int offset, int length);
Parameters:
  • inArray: It is an array of 8-bit unsigned integers.
  • offset: It is an offset in inArray.
  • length: It is the number of elements of inArray to convert.
Return Value: This method returns the string representation in base 64 of length elements of inArray, starting at position offset. Exceptions:
  • ArgumentNullException: If inArray is null.
  • ArgumentOutOfRangeException: If the offset or length is negative OR offset plus length is greater than the length of inArray.
Below programs illustrate the use of Convert.ToBase64String(Byte[], Int32, Int32) Method: Example 1: csharp
// C# program to demonstrate the
// Convert.ToBase64String() 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 (ArgumentOutOfRangeException 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 val = Convert.ToBase64String(bytes,
                             0, bytes.Length);

    // display the converted string
    Console.WriteLine("converted string: {0}", val);
}
}
Output:
For byte1
converted string: AgQGCAoMDhASFA==

For byte2
converted string: ChQeKDI=
Example 2: For ArgumentNullException csharp
// C# program to demonstrate the
// Convert.ToBase64String() Method
using System;

class GFG {

// Main Method
public static void Main()
{
    try {

        // defining and initializing
        // byte1
        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("bye array is null");

        string val = Convert.ToBase64String(null, 0, 10);
        Console.WriteLine("Converted byte value: {0}", val);
    }

    catch (ArgumentNullException e) {

        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }

    catch (ArgumentOutOfRangeException 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 val = Convert.ToBase64String(bytes, 
                             0, bytes.Length);

    // display the converted string
    Console.WriteLine("converted string: {0}", val);
}
}
Output:
For byte1
converted string: AgQGCAoMDhASFA==

bye array is null
Exception Thrown: System.ArgumentNullException
Example 3: For ArgumentOutOfRangeException csharp
// C# program to demonstrate the
// Convert.ToBase64String() Method
using System;

class GFG {

// Main Method
public static void Main()
{
    try {

        // defining and initializing
        // byte1
        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("Length is negative");

        string val = Convert.ToBase64String(byte1, 0, -10);
        
        Console.WriteLine("Converted byte value: {0}", val);
    }

    catch (ArgumentNullException e) {

        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }

    catch (ArgumentOutOfRangeException 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 val = Convert.ToBase64String(bytes,
                             0, bytes.Length);

    // display the converted string
    Console.WriteLine("converted string: {0}", val);
}
}
Output:
For byte1
converted string: AgQGCAoMDhASFA==

Length is negative
Exception Thrown: System.ArgumentOutOfRangeException
Reference:

Next Article

Similar Reads