Convert.ToByte(String, IFormatProvider) Method in C#

The Convert.ToByte(String, IFormatProvider) method in C# converts the specified string representation of a number to an equivalent 8-bit unsigned integer, using specified culture-specific formatting information.

This method is particularly useful when working with string data from different cultures that may use different number formats, such as different decimal separators or thousand separators.

Syntax

Following is the syntax −

public static byte ToByte(string value, IFormatProvider provider);

Parameters

  • value − A string that contains the number to convert. The value must be between 0 and 255.

  • provider − An object that supplies culture-specific formatting information. This can be a CultureInfo, NumberFormatInfo, or null.

Return Value

Returns an 8-bit unsigned integer that is equivalent to the number in value, or 0 (zero) if value is null.

Using Convert.ToByte() with CultureInfo

Example

using System;
using System.Globalization;

public class Demo {
    public static void Main() {
        CultureInfo cultures = new CultureInfo("en-US");
        String str = "5";
        Console.WriteLine("Converted byte value...");
        byte res = Convert.ToByte(str, cultures);
        Console.WriteLine("{0}", res);
        
        // Testing with different values
        string[] values = {"255", "0", "123"};
        foreach (string val in values) {
            byte result = Convert.ToByte(val, cultures);
            Console.WriteLine("'{0}' converts to {1}", val, result);
        }
    }
}

The output of the above code is −

Converted byte value...
5
'255' converts to 255
'0' converts to 0
'123' converts to 123

Using Convert.ToByte() with NumberFormatInfo

Example

using System;
using System.Globalization;

public class Demo {
    public static void Main() {
        NumberFormatInfo nfi = new NumberFormatInfo();
        nfi.NegativeSign = "~";
        
        string[] values = {"100", "200", "50"};
        
        Console.WriteLine("Converting strings to bytes using NumberFormatInfo:");
        foreach (string value in values) {
            try {
                byte result = Convert.ToByte(value, nfi);
                Console.WriteLine("'{0}' -> {1}", value, result);
            }
            catch (Exception ex) {
                Console.WriteLine("'{0}' -> Error: {1}", value, ex.GetType().Name);
            }
        }
    }
}

The output of the above code is −

Converting strings to bytes using NumberFormatInfo:
'100' -> 100
'200' -> 200
'50' -> 50

Handling Invalid Conversions

Example

using System;
using System.Globalization;

public class Demo {
    public static void Main() {
        CultureInfo culture = CultureInfo.InvariantCulture;
        string[] testValues = {"100", "256", "-1", "abc", null};
        
        Console.WriteLine("Testing various string conversions:");
        foreach (string value in testValues) {
            try {
                byte result = Convert.ToByte(value, culture);
                Console.WriteLine("'{0}' -> {1}", value ?? "null", result);
            }
            catch (OverflowException) {
                Console.WriteLine("'{0}' -> OverflowException (out of byte range)", value);
            }
            catch (FormatException) {
                Console.WriteLine("'{0}' -> FormatException (invalid format)", value);
            }
        }
    }
}

The output of the above code is −

Testing various string conversions:
'100' -> 100
'256' -> OverflowException (out of byte range)
'-1' -> OverflowException (out of byte range)
'abc' -> FormatException (invalid format)
'null' -> 0

Common Use Cases

  • Parsing user input − Converting string input from forms or console to byte values.

  • Data processing − Converting string data from files or databases to numeric byte values.

  • Internationalization − Handling number formats from different cultures and locales.

  • Configuration parsing − Converting configuration values stored as strings to byte values.

Conclusion

The Convert.ToByte(String, IFormatProvider) method provides a robust way to convert string representations of numbers to byte values while respecting culture-specific formatting. It handles null values gracefully and throws appropriate exceptions for invalid inputs, making it ideal for data validation and internationalized applications.

Updated on: 2026-03-17T07:04:35+05:30

598 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements