C# | Convert.ToByte(String, IFormatProvider) Method - GeeksforGeeks

02 Sep, 2021
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() Methodusing System;using System.Globalization;class GFG { // Main Methodpublic 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() methodpublic 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);}}
Converted bool value of specified strings: 
 234,  234,  240,  255,  140,  120,
 
Example 2: For FormatException
csharp
// C# program to demonstrate the// Convert.ToByte() Methodusing System;using System.Globalization;class GFG { // Main Methodpublic 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() methodpublic 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);}}
]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() Methodusing System;using System.Globalization; class GFG { // Main Methodpublic 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() methodpublic 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);}}
]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:
https://docs.microsoft.com/en-us/dotnet/api/system.convert.tobyte?view=netframework-4.7.2#System_Convert_ToByte_System_String_System_IFormatProvider_ 
sweetyty
CSharp Convert Class
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
C# | Inheritance
Partial Classes in C#
C# | Generics - Introduction
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
Switch Statement in C#
Linked List Implementation in C#
Convert String to Character Array in C#