C# | Convert.ToBase64String() Method | Set-1
Last Updated :
03 Apr, 2019
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:
using System;
class GFG {
public static void Main()
{
try {
byte [] byte1 = {2, 4, 6, 8, 10,
12, 14, 16, 18, 20};
byte [] byte2 = {10, 20, 30, 40, 50};
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);
}
}
public static void get ( byte [] bytes, string str)
{
Console.WriteLine( "For {0}" , str);
string val = Convert.ToBase64String(bytes,
0, bytes.Length);
Console.WriteLine( "converted string: {0}" , val);
}
}
|
Output:
For byte1
converted string: AgQGCAoMDhASFA==
For byte2
converted string: ChQeKDI=
Example 2: For ArgumentNullException
using System;
class GFG {
public static void Main()
{
try {
byte [] byte1 = {2, 4, 6, 8, 10,
12, 14, 16, 18, 20};
get (byte1, "byte1" );
Console.WriteLine( "" );
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);
}
}
public static void get ( byte [] bytes, string str)
{
Console.WriteLine( "For {0}" , str);
string val = Convert.ToBase64String(bytes,
0, bytes.Length);
Console.WriteLine( "converted string: {0}" , val);
}
}
|
Output:
For byte1
converted string: AgQGCAoMDhASFA==
bye array is null
Exception Thrown: System.ArgumentNullException
Example 3: For ArgumentOutOfRangeException
using System;
class GFG {
public static void Main()
{
try {
byte [] byte1 = {2, 4, 6, 8, 10,
12, 14, 16, 18, 20};
get (byte1, "byte1" );
Console.WriteLine( "" );
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);
}
}
public static void get ( byte [] bytes, string str)
{
Console.WriteLine( "For {0}" , str);
string val = Convert.ToBase64String(bytes,
0, bytes.Length);
Console.WriteLine( "converted string: {0}" , val);
}
}
|
Output:
For byte1
converted string: AgQGCAoMDhASFA==
Length is negative
Exception Thrown: System.ArgumentOutOfRangeException
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...