C# | Convert.ToByte(String, IFormatProvider) Method
Last Updated :
11 Jul, 2025
This method is used to convert the specified string representation of a number to an equivalent 8-bit unsigned integer, using specified culture-specific formatting information.
Syntax:
public static byte ToByte (string value, IFormatProvider provider);
Parameters:
- value: It is a string that contains the number to convert.
- provider: It is an object that supplies culture-specific formatting information.
Return Value: This method returns an 8-bit unsigned integer that is equivalent to value, or zero if value is null.
Exceptions:
- FormatException: If the value does not consist of an optional sign followed by a sequence of digits (0 through 9).
- OverflowException: If the value represents a number that is less than MinValue or greater than MaxValue.
Below programs illustrate the use of Convert.ToByte(String, IFormatProvider) Method:
Example 1:
csharp
// C# program to demonstrate the
// Convert.ToByte() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and initializing String array
string[] values = { "234", "+234", "240",
"255", "140", "120" };
// calling get() Method
Console.WriteLine("Converted bool value "+
"of specified strings: ");
for (int j = 0; j < values.Length; j++) {
get(values[j], cultures);
}
}
catch (FormatException e) {
Console.WriteLine("\n");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (OverflowException e) {
Console.WriteLine("\n");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s, CultureInfo cultures)
{
// converting string to specified bool
byte val = Convert.ToByte(s, cultures);
// display the converted string
Console.Write(" {0}, ", val);
}
}
Output: Converted bool value of specified strings:
234, 234, 240, 255, 140, 120,
Example 2: For FormatException
csharp
// C# program to demonstrate the
// Convert.ToByte() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and initializing String array
string[] values = {"234", "+234", "240",
"255", "140", "A"};
// calling get() Method
Console.WriteLine("]n Converted bool value "+
"of specified strings: ");
for (int j = 0; j < values.Length; j++) {
get(values[j], cultures);
}
}
catch (FormatException e) {
Console.WriteLine("\n\nvalue consist invalid format");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (OverflowException e) {
Console.WriteLine("\n value represents a "+
"number that is less than MinValue");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s,
CultureInfo cultures)
{
// converting string to specified bool
byte val = Convert.ToByte(s, cultures);
// display the converted string
Console.Write(" {0}, ", val);
}
}
Output: ]n Converted bool value of specified strings:
234, 234, 240, 255, 140,
value consist invalid format
Exception Thrown: System.FormatException
Example 3: For OverflowException
csharp
// C# program to demonstrate the
// Convert.ToByte() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and initializing String array
string[] values = {"234", "+234", "240",
"255", "140", "-1" };
// calling get() Method
Console.WriteLine("]n Converted bool value"+
" of specified strings: ");
for (int j = 0; j < values.Length; j++) {
get(values[j], cultures);
}
}
catch (FormatException e) {
Console.WriteLine("\n\nvalue consist invalid format");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (OverflowException e) {
Console.WriteLine("\n\nvalue represents a number"+
" that is less than MinValue");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s, CultureInfo cultures)
{
// converting string to specified bool
byte val = Convert.ToByte(s, cultures);
// display the converted string
Console.Write(" {0}, ", val);
}
}
Output: ]n Converted bool value of specified strings:
234, 234, 240, 255, 140,
value represents a number that is less than MinValue
Exception Thrown: System.OverflowException
Reference:
Similar Reads
C# | Convert.ToSByte(String, IFormatProvider) Method This method is used to convert the specified string representation of a number to an equivalent 8-bit signed integer, using the specified culture-specific formatting information. Syntax: public static sbyte ToSByte (string value, IFormatProvider provider); Parameters: value: It is a string that cont
4 min read
C# | Convert.ToDouble(String, IFormatProvider) Method This method is used to converts the specified string representation of a number to an equivalent double-precision floating-point number, using the specified culture-specific formatting information. Syntax: public static double ToDouble (string value, IFormatProvider provider); Parameters: value: It
4 min read
C# | Convert.ToBoolean(String, IFormatProvider) Method This method is used to convert the specified string representation of a logical value to its Boolean equivalent, using the specified culture-specific formatting information.Syntax: public static bool ToBoolean (string value, IFormatProvider provider); Parameters: value: It is a string that contains
2 min read
C# | Convert.ToInt32(String, IFormatProvider) Method This method is used to converts the specified string representation of a number to an equivalent 32-bit signed integer, using the specified culture-specific formatting information. Syntax: public static int ToInt32 (string value, IFormatProvider provider); Parameters: value: It is a string that cont
4 min read
C# | Convert.ToInt64(String, IFormatProvider) Method This method is used to converts the specified string representation of a number to an equivalent 64-bit signed integer, using the specified culture-specific formatting information. Syntax: public static long ToInt64 (string value, IFormatProvider provider); Parameters: value: It is a string that con
4 min read
C# | Convert.ToInt16(String, IFormatProvider) Method This method is used to convert the specified string representation of a number to an equivalent 16-bit signed integer, using the specified culture-specific formatting information. Syntax: public static short ToInt16 (string value, IFormatProvider provider); Parameters: value: It is a string that con
4 min read