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
Convert.ToSByte Method in C#
The Convert.ToSByte method in C# converts a specified value to an 8-bit signed integer (sbyte). The sbyte data type can store values from -128 to 127 and is useful when you need to work with small signed integers while conserving memory.
Syntax
The Convert.ToSByte method has several overloads to handle different data types −
Convert.ToSByte(value) Convert.ToSByte(value, IFormatProvider) Convert.ToSByte(value, fromBase)
Parameters
value − The value to be converted to
sbyte. Can be of various types includingbool,char,decimal,double,float,int,string, etc.provider − An object that supplies culture-specific formatting information (optional).
fromBase − The base of the number in value (2, 8, 10, or 16) when converting from string.
Return Value
Returns an sbyte equivalent of the specified value. Floating-point values are rounded to the nearest integer using banker's rounding.
Converting Double to SByte
Example
using System;
public class Demo {
public static void Main() {
double doubleNum = -19.9;
sbyte res = Convert.ToSByte(doubleNum);
Console.WriteLine("Converted {0} to {1}", doubleNum, res);
double positiveNum = 45.7;
sbyte res2 = Convert.ToSByte(positiveNum);
Console.WriteLine("Converted {0} to {1}", positiveNum, res2);
}
}
The output of the above code is −
Converted -19.9 to -20 Converted 45.7 to 46
Converting Different Data Types
Example
using System;
public class Demo {
public static void Main() {
// Converting string to sbyte
string str = "100";
sbyte fromString = Convert.ToSByte(str);
Console.WriteLine("String '{0}' to SByte: {1}", str, fromString);
// Converting boolean to sbyte
bool flag = true;
sbyte fromBool = Convert.ToSByte(flag);
Console.WriteLine("Boolean {0} to SByte: {1}", flag, fromBool);
// Converting char to sbyte
char ch = 'A';
sbyte fromChar = Convert.ToSByte(ch);
Console.WriteLine("Char '{0}' to SByte: {1}", ch, fromChar);
}
}
The output of the above code is −
String '100' to SByte: 100 Boolean True to SByte: 1 Char 'A' to SByte: 65
Handling Overflow Exceptions
When the value is outside the range of sbyte (-128 to 127), an OverflowException is thrown −
Example
using System;
public class Demo {
public static void Main() {
try {
int largeValue = 200;
sbyte result = Convert.ToSByte(largeValue);
Console.WriteLine("Converted {0} to {1}", largeValue, result);
}
catch (OverflowException ex) {
Console.WriteLine("OverflowException: {0}", ex.Message);
}
// Valid conversion within range
int validValue = -100;
sbyte validResult = Convert.ToSByte(validValue);
Console.WriteLine("Converted {0} to {1}", validValue, validResult);
}
}
The output of the above code is −
OverflowException: Value was either too large or too small for a signed byte. Converted -100 to -100
Conclusion
The Convert.ToSByte method provides a convenient way to convert various data types to sbyte with automatic rounding for floating-point values. Always handle potential OverflowException when converting values that might exceed the -128 to 127 range.
