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
Byte.ToString() Method in C#
The Byte.ToString() method in C# converts the value of the current Byte object to its equivalent string representation. This method is inherited from the Object class and overridden to provide byte-specific string conversion.
Syntax
Following is the syntax for the basic ToString() method −
public override string ToString();
The Byte class also provides overloaded versions that accept format specifiers −
public string ToString(string format); public string ToString(IFormatProvider provider); public string ToString(string format, IFormatProvider provider);
Using Basic ToString() Method
The parameterless ToString() method converts a byte value to its decimal string representation −
using System;
public class Demo {
public static void Main() {
byte val1, val2;
val1 = Byte.MinValue;
val2 = 15;
Console.WriteLine("Value1 (Byte) = " + val1.ToString());
Console.WriteLine("Value2 (Byte) = " + val2.ToString());
Console.WriteLine("Maximum Byte Value = " + Byte.MaxValue.ToString());
}
}
The output of the above code is −
Value1 (Byte) = 0 Value2 (Byte) = 15 Maximum Byte Value = 255
Using Format Specifiers
You can use format specifiers to control how the byte value is converted to string −
using System;
public class Demo {
public static void Main() {
byte value = 255;
Console.WriteLine("Decimal: " + value.ToString());
Console.WriteLine("Hexadecimal (lowercase): " + value.ToString("x"));
Console.WriteLine("Hexadecimal (uppercase): " + value.ToString("X"));
Console.WriteLine("Binary representation: " + Convert.ToString(value, 2));
Console.WriteLine("With leading zeros: " + value.ToString("D3"));
}
}
The output of the above code is −
Decimal: 255 Hexadecimal (lowercase): ff Hexadecimal (uppercase): FF Binary representation: 11111111 With leading zeros: 255
Common Format Specifiers
| Format Specifier | Description | Example Output (for byte 255) |
|---|---|---|
| "D" or "d" | Decimal format | 255 |
| "X" or "x" | Hexadecimal format | FF or ff |
| "D3" | Decimal with minimum 3 digits | 255 |
| "X2" | Hexadecimal with 2 digits | FF |
Practical Example with Array
Here's a practical example showing how to convert an array of bytes to their string representations −
using System;
public class Demo {
public static void Main() {
byte[] byteArray = {10, 25, 100, 255};
Console.WriteLine("Byte values in different formats:");
foreach(byte b in byteArray) {
Console.WriteLine($"Decimal: {b.ToString()}, Hex: {b.ToString("X2")}, Padded: {b.ToString("D3")}");
}
}
}
The output of the above code is −
Byte values in different formats: Decimal: 10, Hex: 0A, Padded: 010 Decimal: 25, Hex: 19, Padded: 025 Decimal: 100, Hex: 64, Padded: 100 Decimal: 255, Hex: FF, Padded: 255
Conclusion
The Byte.ToString() method provides flexible string conversion for byte values. It supports various format specifiers for decimal, hexadecimal, and padded representations, making it essential for data formatting and display operations in C# applications.
