C# | BitConverter.ToString(Byte[]) Method

This method is used to convert the numeric value of each element of a specified array of bytes to its equivalent hexadecimal string representation.

Syntax:

public static string ToString (byte[] value);

Here, the value is an array of bytes.



Return Value: This method returns a string of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value. For example, “6E-1D-9A-00”.

Exception: This method throws ArgumentNullException if the byte array or you can say value is null.

Below programs illustrate the use of BitConverter.ToString(Byte[]) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// BitConverter.ToString(Byte[]);
// Method
using System;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Define an array of byte values.
            byte[] array1 = {0, 1, 2, 4, 8, 16,
                             32, 64, 128, 255};
  
            // Display the values of the myArr.
            Console.Write("Initial Array: ");
  
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(array1);
  
            // Getting hex string of byte values
            string value = BitConverter.ToString(array1);
  
            // Display the string
            Console.Write("string: ");
            Console.Write("{0}", value);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(byte[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.Write("{0} ", myArr[i]);
        }
        Console.WriteLine();
        Console.WriteLine();
    }
}

chevron_right


Output:

Initial Array: 0 1 2 4 8 16 32 64 128 255 

string: 00-01-02-04-08-10-20-40-80-FF

Example 2: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// BitConverter.ToString(Byte[]);
// Method
using System;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Define an array of byte values.
            byte[] array1 = null;
  
            // Getting hex string of byte values
            string value = BitConverter.ToString(array1);
  
            // Display the string
            Console.Write("string: ");
            Console.Write("{0}", value);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

Exception Thrown: System.ArgumentNullException

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.