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

The Convert.ToSByte(String, IFormatProvider) method in C# converts the specified string representation of a number to an equivalent 8-bit signed integer, using the specified culture-specific formatting information. This method is particularly useful when working with numeric strings that may have different formatting conventions based on locale.

Syntax

Following is the syntax −

public static sbyte ToSByte(string value, IFormatProvider provider);

Parameters

  • value − A string that contains the number to convert. The value must be between -128 and 127.

  • provider − An object that supplies culture-specific formatting information. This can be null for invariant culture.

Return Value

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

Using Convert.ToSByte() with Different Cultures

Example

using System;
using System.Globalization;

public class Demo {
   public static void Main() {
      CultureInfo cultures = new CultureInfo("en-US");
      String str = "-2";
      Console.WriteLine("Converted sbyte value...");
      sbyte res = Convert.ToSByte(str, cultures);
      Console.Write("{0}", res);
   }
}

The output of the above code is −

Converted sbyte value...
-2

Using Convert.ToSByte() with Multiple Culture Formats

Example

using System;
using System.Globalization;

public class Demo {
   public static void Main() {
      string[] values = {"127", "-128", "0", "100"};
      IFormatProvider[] providers = {
         new CultureInfo("en-US"),
         new CultureInfo("fr-FR"), 
         CultureInfo.InvariantCulture
      };

      foreach (string value in values) {
         Console.WriteLine("Converting '{0}':", value);
         foreach (IFormatProvider provider in providers) {
            try {
               sbyte result = Convert.ToSByte(value, provider);
               Console.WriteLine("  {0}: {1}", 
                  provider.ToString(), result);
            }
            catch (Exception ex) {
               Console.WriteLine("  {0}: {1}", 
                  provider.ToString(), ex.GetType().Name);
            }
         }
         Console.WriteLine();
      }
   }
}

The output of the above code is −

Converting '127':
  en-US: 127
  fr-FR: 127
  : 127

Converting '-128':
  en-US: -128
  fr-FR: -128
  : -128

Converting '0':
  en-US: 0
  fr-FR: 0
  : 0

Converting '100':
  en-US: 100
  fr-FR: 100
  : 100

Handling Overflow and Format Exceptions

Example

using System;
using System.Globalization;

public class Demo {
   public static void Main() {
      string[] testValues = {"127", "128", "-129", "abc", null};
      
      foreach (string value in testValues) {
         try {
            sbyte result = Convert.ToSByte(value, CultureInfo.InvariantCulture);
            Console.WriteLine("'{0}' converted to: {1}", 
               value ?? "null", result);
         }
         catch (OverflowException) {
            Console.WriteLine("'{0}' causes OverflowException (out of sbyte range)", value);
         }
         catch (FormatException) {
            Console.WriteLine("'{0}' causes FormatException (invalid format)", value);
         }
         catch (ArgumentNullException) {
            Console.WriteLine("null value converted to: 0");
         }
      }
   }
}

The output of the above code is −

'127' converted to: 127
'128' causes OverflowException (out of sbyte range)
'-129' causes OverflowException (out of sbyte range)
'abc' causes FormatException (invalid format)
null value converted to: 0

Conclusion

The Convert.ToSByte(String, IFormatProvider) method provides a robust way to convert string representations of numbers to signed bytes with culture-specific formatting support. It handles null values gracefully and throws appropriate exceptions for invalid formats or values outside the sbyte range (-128 to 127).

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

210 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements