Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# BitConverter.ToString(Byte[]) Method
The BitConverter.ToString() method in C# converts an array of bytes to its hexadecimal string representation. Each byte is converted to a two-digit uppercase hexadecimal value, with hyphens separating each pair.
Syntax
Following is the syntax for the BitConverter.ToString() method −
public static string ToString(byte[] value);
Parameters
value − An array of bytes to convert to hexadecimal string representation.
Return Value
Returns a string containing hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in the byte array.
Using BitConverter.ToString() with Byte Array
Example
using System;
public class Demo {
public static void Main() {
byte[] arr = {0, 10, 2, 5, 32, 45};
int count = arr.Length;
Console.Write("Byte Array... ");
for (int i = 0; i < count; i++) {
Console.Write("<br>" + arr[i]);
}
Console.WriteLine("\nByte Array (String representation) = {0}",
BitConverter.ToString(arr));
}
}
The output of the above code is −
Byte Array... 0 10 2 5 32 45 Byte Array (String representation) = 00-0A-02-05-20-2D
Converting Bytes Back to Numeric Values
Example
using System;
public class Demo {
public static void Main() {
byte[] arr = {0, 0, 7, 10, 18, 20, 25, 26, 32};
int count = arr.Length;
Console.Write("Byte Array... ");
for (int i = 0; i < count; i++) {
Console.Write("<br>" + arr[i]);
}
Console.WriteLine("\nByte Array (String representation) = " + BitConverter.ToString(arr));
for (int i = 0; i < arr.Length - 1; i = i + 2) {
short res = BitConverter.ToInt16(arr, i);
Console.WriteLine("\nValue = " + arr[i]);
Console.WriteLine("Result = " + res);
}
}
}
The output of the above code is −
Byte Array... 0 0 7 10 18 20 25 26 32 Byte Array (String representation) = 00-00-07-0A-12-14-19-1A-20 Value = 0 Result = 0 Value = 7 Result = 2567 Value = 18 Result = 5138 Value = 25 Result = 6681
Working with Different Byte Values
Example
using System;
public class Demo {
public static void Main() {
// Different byte values including maximum
byte[] bytes = {0, 255, 127, 64, 192};
Console.WriteLine("Original bytes:");
foreach (byte b in bytes) {
Console.Write(b + " ");
}
Console.WriteLine("\nHexadecimal representation:");
string hexString = BitConverter.ToString(bytes);
Console.WriteLine(hexString);
// Remove hyphens for continuous hex string
string continuousHex = hexString.Replace("-", "");
Console.WriteLine("Without hyphens: " + continuousHex);
}
}
The output of the above code is −
Original bytes: 0 255 127 64 192 Hexadecimal representation: 00-FF-7F-40-C0 Without hyphens: 00FF7F40C0
Conclusion
The BitConverter.ToString() method provides an easy way to convert byte arrays to readable hexadecimal strings. Each byte is represented as a two-digit uppercase hexadecimal value separated by hyphens, making it useful for debugging, data visualization, and converting binary data to string format.
