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

This method is used to convert a subset of an 8-bit unsigned integer array to an equivalent subset of a Unicode character array encoded with base-64 digits. Parameters specify the subsets as offsets in the input and output arrays, and the number of elements in the input array to convert.

Syntax:

public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length, char[] outArray, int offsetOut);

Parameters:

  • inArray: An input array of 8-bit unsigned integers.
  • offsetIn: A position within inArray.
  • length: The number of elements of inArray to convert.
  • outArray: An output array of Unicode characters.
  • offsetOut: A position within outArray.

Return Value: This method returns a 32-bit signed integer containing the number of bytes in outArray.

Exceptions:

  • ArgumentNullException: If the inArray or outArray is null.
  • ArgumentOutOfRangeException: If the offsetIn, offsetOut, or length is negative OR offsetIn plus length is greater than the length of inArray OR offsetOut plus the number of elements to return is greater than the length of outArray.

Below programs illustrate the use of Convert.ToBase64CharArray() Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 (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);
  
    long arrayLength = (long)((4.0d / 3.0d) *
                               bytes.Length);
  
    // If array length is not divisible
    // by 4, go up to the next multiple
    // of 4.
    if (arrayLength % 4 != 0)
        arrayLength += 4 - arrayLength % 4;
  
    // creating object of char array
    char[] base64CharArray = new char[arrayLength];
  
    // converting byte to base 64 string
    int val = Convert.ToBase64CharArray(bytes, 0, 
               bytes.Length, base64CharArray, 0);
  
    Console.WriteLine("Total no of bytes: {0}", val);
  
    // display the base64CharArray
    Console.Write("base64CharArray: ");
    for (int j = 0; j < base64CharArray.Length; j++)
        Console.Write("{0}", base64CharArray[j]);
    Console.WriteLine("");
}
}

chevron_right


Output:

For byte1
Total no of bytes: 16
base64CharArray: AgQGCAoMDhASFA==

For byte2
Total no of bytes: 8
base64CharArray: ChQeKDI=

Example 2: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Convert.FromBase64String(String)
// 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");
  
        // converting base 64 
        // string to byte array
        Console.WriteLine("inArray and outArray are null.");
  
        int val = Convert.ToBase64CharArray(null, 0,
                                       10, null, 0);
                                         
        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);
  
    long arrayLength = (long)((4.0d / 3.0d) * 
                               bytes.Length);
  
    // If array length is not divisible 
    // by 4, go up to the next multiple
    // of 4.
    if (arrayLength % 4 != 0)
        arrayLength += 4 - arrayLength % 4;
  
    // creating object of char array
    char[] base64CharArray = new char[arrayLength];
  
    // converting byte to base 64 string
    int val = Convert.ToBase64CharArray(bytes, 0, 
               bytes.Length, base64CharArray, 0);
  
    Console.WriteLine("Total no of bytes: {0}", val);
  
    // display the base64CharArray
    Console.Write("base64CharArray: ");
    for (int j = 0; j < base64CharArray.Length; j++)
        Console.Write("{0}", base64CharArray[j]);
    Console.WriteLine("\n");
}
}

chevron_right


Output:

For byte1
Total no of bytes: 16
base64CharArray: AgQGCAoMDhASFA==

inArray and outArray are null.
Exception Thrown: System.ArgumentNullException

Example 3: For ArgumentOutOfRangeException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
  
        // converting base 64 string
        // to byte array
        Console.WriteLine("length of inArray is negative");
  
        char[] base64CharArray = new char[10];
        int val = Convert.ToBase64CharArray(byte2, 0,
                            -10, base64CharArray, 0);
                              
        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);
  
    long arrayLength = (long)((4.0d / 3.0d) * bytes.Length);
  
    // If array length is not divisible 
    // by 4, go up to the next multiple 
    // of 4.
    if (arrayLength % 4 != 0)
        arrayLength += 4 - arrayLength % 4;
  
    // creating object of char array
    char[] base64CharArray = new char[arrayLength];
  
    // converting byte to base 64 string
    int val = Convert.ToBase64CharArray(bytes, 0, 
               bytes.Length, base64CharArray, 0);
  
    Console.WriteLine("Total no of bytes: {0}", val);
  
    // display the base64CharArray
    Console.Write("base64CharArray: ");
    for (int j = 0; j < base64CharArray.Length; j++)
        Console.Write("{0}", base64CharArray[j]);
    Console.WriteLine("\n");
}
}

chevron_right


Output:

For byte1
Total no of bytes: 16
base64CharArray: AgQGCAoMDhASFA==

length of inArray is negative
Exception Thrown: System.ArgumentOutOfRangeException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.