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:
// 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(); } } |
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
// 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); } } } |
Exception Thrown: System.ArgumentNullException
Reference:
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | String.Contains() Method
- C# | Uri.IsHexDigit() Method
- C# | Uri.IsBaseOf(Uri) Method
- C# | Method Overriding
- C# | Math.Log() Method
- C# | Uri.FromHex() Method
- C# | Dictionary.Add() Method
- C# | Trim() Method
- C# | Math.Tan() Method
- C# | Math.Cos() Method
- C# | IndexOfAny() Method
- C# | CopyTo() Method
- C# | Math.Sin() Method
- C# | Math.Abs() Method | Set - 2
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.



