Skip to content
Related Articles

Related Articles

C# | Convert.ToByte(String, IFormatProvider) Method
  • Last Updated : 01 Feb, 2019

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 intializing 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);
}
}

chevron_right


Output:

Converted bool value of specified strings: 
 234,  234,  240,  255,  140,  120,

Example 2: For FormatException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 intializing 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);
}
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 intializing 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);
}
}

chevron_right


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:

My Personal Notes arrow_drop_up
Recommended Articles
Page :