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
SByte.ToString() Method in C# with Examples
The SByte.ToString() method in C# is used to convert the numeric value of a signed byte to its equivalent string representation. The sbyte data type represents 8-bit signed integers with values ranging from -128 to 127.
Syntax
Following is the syntax for the basic ToString() method −
public override string ToString();
Following is the syntax for ToString() with format provider −
public string ToString(IFormatProvider provider); public string ToString(string format); public string ToString(string format, IFormatProvider provider);
Return Value
The method returns a string that represents the value of the current sbyte instance.
Using Basic ToString() Method
The simplest form converts an sbyte value to its string representation without any formatting −
using System;
public class Demo {
public static void Main() {
sbyte s1 = 10;
sbyte s2 = -50;
sbyte s3 = 127; // Maximum value for sbyte
sbyte s4 = -128; // Minimum value for sbyte
Console.WriteLine("s1 ToString(): " + s1.ToString());
Console.WriteLine("s2 ToString(): " + s2.ToString());
Console.WriteLine("s3 ToString(): " + s3.ToString());
Console.WriteLine("s4 ToString(): " + s4.ToString());
}
}
The output of the above code is −
s1 ToString(): 10 s2 ToString(): -50 s3 ToString(): 127 s4 ToString(): -128
Using ToString() with Format Strings
You can format the string representation using standard numeric format strings −
using System;
public class Demo {
public static void Main() {
sbyte value = 42;
Console.WriteLine("Default: " + value.ToString());
Console.WriteLine("Decimal (D): " + value.ToString("D"));
Console.WriteLine("Hexadecimal (X): " + value.ToString("X"));
Console.WriteLine("Binary equivalent: " + Convert.ToString(value, 2));
Console.WriteLine("With leading zeros: " + value.ToString("D3"));
sbyte negative = -15;
Console.WriteLine("Negative value: " + negative.ToString());
Console.WriteLine("Negative hex: " + negative.ToString("X2"));
}
}
The output of the above code is −
Default: 42 Decimal (D): 42 Hexadecimal (X): 2A Binary equivalent: 101010 With leading zeros: 042 Negative value: -15 Negative hex: F1
Practical Example with Range Validation
Here's a practical example showing ToString() usage with range checking −
using System;
public class Demo {
public static void Main() {
sbyte[] values = { -128, -1, 0, 1, 50, 127 };
Console.WriteLine("SByte Range: " + sbyte.MinValue + " to " + sbyte.MaxValue);
Console.WriteLine("Values and their string representations:");
foreach (sbyte value in values) {
string strValue = value.ToString();
Console.WriteLine($"Value: {value,4} | String: '{strValue}' | Length: {strValue.Length}");
}
// Demonstrating edge cases
sbyte maxVal = sbyte.MaxValue;
sbyte minVal = sbyte.MinValue;
Console.WriteLine("\nEdge cases:");
Console.WriteLine("Max value as string: " + maxVal.ToString());
Console.WriteLine("Min value as string: " + minVal.ToString());
}
}
The output of the above code is −
SByte Range: -128 to 127 Values and their string representations: Value: -128 | String: '-128' | Length: 4 Value: -1 | String: '-1' | Length: 2 Value: 0 | String: '0' | Length: 1 Value: 1 | String: '1' | Length: 1 Value: 50 | String: '50' | Length: 2 Value: 127 | String: '127' | Length: 3 Edge cases: Max value as string: 127 Min value as string: -128
Comparison with Other Methods
| Method | Purpose | Example Output |
|---|---|---|
| ToString() | Convert to string | "42" |
| ToString("X") | Hexadecimal format | "2A" |
| ToString("D3") | Decimal with padding | "042" |
| GetHashCode() | Get hash code | Integer value |
Conclusion
The SByte.ToString() method provides a simple way to convert signed byte values to their string representations. It supports various format strings for different output formats like decimal, hexadecimal, and padded representations, making it versatile for different programming scenarios.
