System.Convert.ToSByte 方法 (String, Int32)

方法描述

将指定基的数字的字符串表示形式转换为等效的 8 位带符号整数。

语法定义(C# System.Convert.ToSByte 方法 (String, Int32) 的用法)

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

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-String 包含要转换的数字的字符串。
fromBase System-Int32 value 中数字的基数,它必须是 2、8、10 或 16。
返回值 System.SByte 与 value 中数字等效的 8 位带符号整数,如果 value 为 null,则为 0(零)。

提示和注释

如果 fromBase 为 16,则可以在 value 参数指定的数字前面加上“0x”或“0X”。

由于不是以 10 为基数的数值表示形式不支持负号,因此 ToSByte(String, Int32) 方法假定负数使用的是 2 的补数表示形式。 换句话说,此方法总是将字节的最高序位(位 7)解释为其符号位。 因此,可以编写代码以将超出 SByte 数据类型的范围的不是以 10 为基数的数字转换为 SByte 值,从而不让该方法引发异常。 下面的示例将 MaxValue 转换为对应的十六进制字符串表示形式,然后调用 ToSByte(String, Int32) 方法。 该方法将显示消息“0xff converts to -1”(0xff 转换为 -1),而不是引发异常。

C#

VB

复制

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

}

在执行二元运算或数值转换时,开发人员始终需要负责验证方法是否使用了适当的数值表示形式来解释特定值。 正如下面的示例所演示的那样,可以通过在将一个值转换为对应的十六进制字符串表示形式之前,先确定该值是表示一个无符号类型还是一个有符号类型,从而确保该方法正确地处理溢出。 如果原始值是无符号类型,而转换回有符号字节的结果是一个带有符号位的值,则将引发异常。

C#

VB

复制

// 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.

System.Convert.ToSByte 方法 (String, Int32)例子

下面的示例尝试将字符串数组的元素解释为用二进制、八进制和十六进制表示的数值,以便将它们转换为无符号的字节。

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

异常

异常 异常描述
ArgumentException
  • fromBase 不是 2、8、10 或 16。
  • value,它表示一个非 10 为基的有符号数,前面带一个负号。
FormatException value 包含的一个字符不是 fromBase 指定的基中的有效数字。 如果 value 中的第一个字符无效,异常消息则指示没有可转换的数字;否则,该消息将指示 value 包含无效的尾随字符。
OverflowException
  • value,它表示一个非 10 为基的有符号数,前面带一个负号。
  • value 表示小于 SByte.MinValue 或大于 SByte.MaxValue 的数字。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。