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.ToByte Method in C#
The Convert.ToByte method in C# converts a specified value to an 8-bit unsigned integer (byte). A byte can hold values from 0 to 255, making it useful for representing small integers, ASCII characters, and binary data.
This method provides a safe way to convert various data types to byte values, with built-in overflow checking that throws an OverflowException if the value is outside the valid byte range.
Syntax
Following are the common overloads of the Convert.ToByte method −
Convert.ToByte(object value) Convert.ToByte(string value) Convert.ToByte(char value) Convert.ToByte(int value) Convert.ToByte(double value)
Parameters
value − The value to be converted to a byte. Can be of various types including string, char, int, double, bool, etc.
Return Value
Returns an 8-bit unsigned integer (byte) equivalent to the specified value, or zero if the value is null.
Converting Characters to Bytes
Characters are converted to their ASCII byte values −
using System;
public class Demo {
public static void Main() {
char[] charVal = { 'p', 'q', 'r', 's' };
foreach (char c in charVal) {
byte byteVal = Convert.ToByte(c);
Console.WriteLine("{0} is converted to = {1}", c, byteVal);
}
}
}
The output of the above code is −
p is converted to = 112 q is converted to = 113 r is converted to = 114 s is converted to = 115
Converting Different Data Types
The method can convert various data types to byte values −
using System;
public class Demo {
public static void Main() {
// Converting string to byte
string strVal = "255";
byte byteFromString = Convert.ToByte(strVal);
Console.WriteLine("String '{0}' to byte: {1}", strVal, byteFromString);
// Converting boolean to byte
bool boolVal = true;
byte byteFromBool = Convert.ToByte(boolVal);
Console.WriteLine("Boolean {0} to byte: {1}", boolVal, byteFromBool);
// Converting double to byte
double doubleVal = 100.75;
byte byteFromDouble = Convert.ToByte(doubleVal);
Console.WriteLine("Double {0} to byte: {1}", doubleVal, byteFromDouble);
}
}
The output of the above code is −
String '255' to byte: 255 Boolean True to byte: 1 Double 100.75 to byte: 101
Handling Overflow Exceptions
Values outside the byte range (0-255) will throw an OverflowException −
using System;
public class Demo {
public static void Main() {
try {
int validValue = 200;
byte result1 = Convert.ToByte(validValue);
Console.WriteLine("Valid conversion: {0} to {1}", validValue, result1);
int invalidValue = 300; // Outside byte range
byte result2 = Convert.ToByte(invalidValue);
}
catch (OverflowException ex) {
Console.WriteLine("OverflowException: " + ex.Message);
}
}
}
The output of the above code is −
Valid conversion: 200 to 200 OverflowException: Value was either too large or too small for an unsigned byte.
Common Use Cases
| Data Type | Conversion Result | Example |
|---|---|---|
| String "0"-"255" | Numeric byte value | "123" ? 123 |
| Boolean | 1 for true, 0 for false | true ? 1 |
| Char | ASCII value | 'A' ? 65 |
| Double/Float | Rounded to nearest integer | 42.7 ? 43 |
Conclusion
The Convert.ToByte method provides a reliable way to convert various data types to byte values with automatic overflow checking. It's particularly useful when working with ASCII characters, small integers, or when you need safe type conversion with exception handling for out-of-range values.
