Convert.ToSByte Method (String, Int32)

Converts the string representation of a number in a specified base to an equivalent 8-bit signed integer.

This API is not CLS-compliant. 

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[CLSCompliantAttribute(false)]
public static sbyte ToSByte(
	string value,
	int fromBase
)

Parameters

value
Type: System.String

A string that contains the number to convert.

fromBase
Type: System.Int32

The base of the number in value, which must be 2, 8, 10, or 16.

Return Value

Type: System.SByte
An 8-bit signed integer that is equivalent to the number in value, or 0 (zero) if value is null.

ExceptionCondition
ArgumentException

fromBase is not 2, 8, 10, or 16.

-or-

value, which represents a non-base 10 signed number, is prefixed with a negative sign.

ArgumentOutOfRangeException

value is String.Empty.

FormatException

value contains a character that is not a valid digit in the base specified by fromBase. The exception message indicates that there are no digits to convert if the first character in value is invalid; otherwise, the message indicates that value contains invalid trailing characters.

OverflowException

value, which represents a non-base 10 signed number, is prefixed with a negative sign.

-or-

value represents a number that is less than SByte.MinValue or greater than SByte.MaxValue.

If fromBase is 16, you can prefix the number specified by the value parameter with "0x" or "0X".

Because the negative sign is not supported for non-base 10 numeric representations, the ToSByte(String, Int32) method assumes that negative numbers use two’s complement representation. In other words, the method always interprets the high-order bit of a byte (bit 7) as its sign bit. As a result, it is possible to write code in which a non-base 10 number that is out of the range of the SByte data type is converted to an SByte value without the method throwing an exception. The following example converts MaxValue to its hexadecimal string representation, and then calls the ToSByte(String, Int32) method. Instead of throwing an exception, the method displays the message, "0xff converts to -1."

// Create a hexadecimal value out of range of the SByte type. 
string value = Convert.ToString(byte.MaxValue, 16);
// Convert it back to a number. 
try
{
   sbyte number = Convert.ToSByte(value, 16);
   Console.WriteLine("0x{0} converts to {1}.", value, number);
}   
catch (OverflowException)
{
   Console.WriteLine("Unable to convert '0x{0}' to a signed byte.", value);
}   

When performing binary operations or numeric conversions, it is always the responsibility of the developer to verify that a method is using the appropriate numeric representation to interpret a particular value. As the following example illustrates, you can ensure that the method handles overflows appropriately by first determining whether a value represents an unsigned or a signed type when converting it to its hexadecimal string representation. Throw an exception if the original value was an unsigned type but the conversion back to a signed byte yields a value whose sign bit is on.

// Create a hexadecimal value out of range of the SByte type. 
byte sourceNumber = byte.MaxValue;
bool isSigned = Math.Sign(Convert.ToDouble(sourceNumber.GetType().GetField("MinValue").GetValue(null))) == -1;
string value = Convert.ToString(sourceNumber, 16);
sbyte targetNumber;
try
{
   targetNumber = Convert.ToSByte(value, 16);
   if (! isSigned && ((targetNumber & 0x80) != 0))
      throw new OverflowException();
   else 
      Console.WriteLine("0x{0} converts to {1}.", value, targetNumber);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert '0x{0}' to a signed byte.", value);
} 
// Displays the following to the console: 
//    Unable to convert '0xff' to a signed byte.     

The following example attempts to interpret the elements in a string array as the binary, octal, and hexadecimal representation of numeric values in order to convert them to unsigned bytes.

using System;

public class Example
{
   public static void Main()
   {
      int[] baseValues = { 2, 8, 16};
      string[] values = { "FF", "81", "03", "11", "8F", "01", "1C", "111", 
                          "123", "18A" }; 

      // Convert to each supported base. 
      foreach (int baseValue in baseValues)
      {
         Console.WriteLine("Converting strings in base {0}:", baseValue);
         foreach (string value in values)
         {
            Console.Write("   '{0,-5}  -->  ", value + "'");
            try {
               Console.WriteLine(Convert.ToSByte(value, baseValue));
            }   
            catch (FormatException) {
               Console.WriteLine("Bad Format");
            }   
            catch (OverflowException) {
               Console.WriteLine("Out of Range");
            }
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output: 
//       Converting strings in base 2: 
//          'FF'    -->  Bad Format 
//          '81'    -->  Bad Format 
//          '03'    -->  Bad Format 
//          '11'    -->  3 
//          '8F'    -->  Bad Format 
//          '01'    -->  1 
//          '1C'    -->  Bad Format 
//          '111'   -->  7 
//          '123'   -->  Bad Format 
//          '18A'   -->  Bad Format 
//        
//       Converting strings in base 8: 
//          'FF'    -->  Bad Format 
//          '81'    -->  Bad Format 
//          '03'    -->  3 
//          '11'    -->  9 
//          '8F'    -->  Bad Format 
//          '01'    -->  1 
//          '1C'    -->  Bad Format 
//          '111'   -->  73 
//          '123'   -->  83 
//          '18A'   -->  Bad Format 
//        
//       Converting strings in base 16: 
//          'FF'    -->  -1 
//          '81'    -->  -127 
//          '03'    -->  3 
//          '11'    -->  17 
//          '8F'    -->  -113 
//          '01'    -->  1 
//          '1C'    -->  28 
//          '111'   -->  Out of Range 
//          '123'   -->  Out of Range 
//          '18A'   -->  Out of Range

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft