当前位置: 首页>>代码示例>>C#>>正文


C# Convert.ToSByte方法代码示例

本文整理汇总了C#中System.Convert.ToSByte方法的典型用法代码示例。如果您正苦于以下问题:C# Convert.ToSByte方法的具体用法?C# Convert.ToSByte怎么用?C# Convert.ToSByte使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Convert的用法示例。


在下文中一共展示了Convert.ToSByte方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
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();
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:32,代码来源:Convert.ToSByte

输出:

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

示例2:

// 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);
}
开发者ID:.NET开发者,项目名称:System,代码行数:12,代码来源:Convert.ToSByte

示例3: if

// 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.
开发者ID:.NET开发者,项目名称:System,代码行数:19,代码来源:Convert.ToSByte

示例4: GetExceptionType

// Example of the Convert.ToSByte( string ) and 
// Convert.ToSByte( string, IFormatProvider ) methods.
using System;
using System.Globalization;

class ToSByteProviderDemo
{
    static string format = "{0,-20}{1,-20}{2}";

     // Get the exception type name; remove the namespace prefix.
    static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    static void ConvertToSByte( string numericStr, 
        IFormatProvider provider )
    {
        object defaultValue;
        object providerValue;

        // Convert numericStr to SByte without a format provider.
        try
        {
            defaultValue = Convert.ToSByte( numericStr );
        }
        catch( Exception ex )
        {
            defaultValue = GetExceptionType( ex );
        }

        // Convert numericStr to SByte with a format provider.
        try
        {
            providerValue = Convert.ToSByte( numericStr, provider );
        }
        catch( Exception ex )
        {
            providerValue = GetExceptionType( ex );
        }

        Console.WriteLine( format, numericStr, 
            defaultValue, providerValue );
    }

    public static void Main( )
    {
        // Create a NumberFormatInfo object and set several of its
        // properties that apply to numbers.
        NumberFormatInfo provider = new NumberFormatInfo();

        // These properties affect the conversion.
        provider.NegativeSign = "neg ";
        provider.PositiveSign = "pos ";

        // These properties do not affect the conversion.
        // The input string cannot have decimal and group separators.
        provider.NumberDecimalSeparator = ".";
        provider.NumberNegativePattern = 0;

        Console.WriteLine("This example of\n" +
            "  Convert.ToSByte( string ) and \n" +
            "  Convert.ToSByte( string, IFormatProvider ) " +
            "\ngenerates the following output. It converts " +
            "several strings to \nSByte values, using " +
            "default formatting or a NumberFormatInfo object.\n" );
        Console.WriteLine( format, "String to convert", 
            "Default/exception", "Provider/exception" );
        Console.WriteLine( format, "-----------------", 
            "-----------------", "------------------" );

        // Convert strings, with and without an IFormatProvider.
        ConvertToSByte( "123", provider );
        ConvertToSByte( "+123", provider );
        ConvertToSByte( "pos 123", provider );
        ConvertToSByte( "-123", provider );
        ConvertToSByte( "neg 123", provider );
        ConvertToSByte( "123.", provider );
        ConvertToSByte( "(123)", provider );
        ConvertToSByte( "128", provider );
        ConvertToSByte( "-129", provider );
    }
}

/*
This example of
  Convert.ToSByte( string ) and
  Convert.ToSByte( string, IFormatProvider )
generates the following output. It converts several strings to
SByte values, using default formatting or a NumberFormatInfo object.

String to convert   Default/exception   Provider/exception
-----------------   -----------------   ------------------
123                 123                 123
+123                123                 FormatException
pos 123             FormatException     123
-123                -123                FormatException
neg 123             FormatException     -123
123.                FormatException     FormatException
(123)               FormatException     FormatException
128                 OverflowException   OverflowException
-129                OverflowException   FormatException
*/
开发者ID:.NET开发者,项目名称:System,代码行数:105,代码来源:Convert.ToSByte

示例5: if

//引入命名空间
using System;
using System.Globalization;

public enum SignBit { Negative=-1, Zero=0, Positive=1 };

public struct ByteString : IConvertible
{
   private SignBit signBit;
   private string byteString;
   
   public SignBit Sign
   { 
      set { signBit = value; }
      get { return signBit; }
   }

   public string Value
   { 
      set {
         if (value.Trim().Length > 2)
            throw new ArgumentException("The string representation of a byte cannot have more than two characters.");
         else
            byteString = value;
      }
      get { return byteString; }
   }
   
   // IConvertible implementations.
   public TypeCode GetTypeCode() {
      return TypeCode.Object;
   }
   
   public bool ToBoolean(IFormatProvider provider)
   {
      if (signBit == SignBit.Zero)
         return false;
      else
         return true;
   } 
   
   public byte ToByte(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         throw new OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToSByte(byteString, 16)));
      else
         return Byte.Parse(byteString, NumberStyles.HexNumber);
   }
   
   public char ToChar(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative) { 
         throw new OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToSByte(byteString, 16)));
      }
      else {
         byte byteValue = Byte.Parse(this.byteString, NumberStyles.HexNumber);
         return Convert.ToChar(byteValue);
      }                
   } 
   
   public DateTime ToDateTime(IFormatProvider provider)
   {
      throw new InvalidCastException("ByteString to DateTime conversion is not supported.");
   }
   
   public decimal ToDecimal(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative) 
      {
         sbyte byteValue = SByte.Parse(byteString, NumberStyles.HexNumber);
         return Convert.ToDecimal(byteValue);
      }
      else 
      {
         byte byteValue = Byte.Parse(byteString, NumberStyles.HexNumber);
         return Convert.ToDecimal(byteValue);
      }
   }
   
   public double ToDouble(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToDouble(SByte.Parse(byteString, NumberStyles.HexNumber));
      else
         return Convert.ToDouble(Byte.Parse(byteString, NumberStyles.HexNumber));
   }   
   
   public short ToInt16(IFormatProvider provider) 
   {
      if (signBit == SignBit.Negative)
         return Convert.ToInt16(SByte.Parse(byteString, NumberStyles.HexNumber));
      else
         return Convert.ToInt16(Byte.Parse(byteString, NumberStyles.HexNumber));
   }
   
   public int ToInt32(IFormatProvider provider) 
   {
      if (signBit == SignBit.Negative)
         return Convert.ToInt32(SByte.Parse(byteString, NumberStyles.HexNumber));
      else
         return Convert.ToInt32(Byte.Parse(byteString, NumberStyles.HexNumber));
   }
   
   public long ToInt64(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToInt64(SByte.Parse(byteString, NumberStyles.HexNumber));
      else
         return Convert.ToInt64(Byte.Parse(byteString, NumberStyles.HexNumber));
   }
   
   public sbyte ToSByte(IFormatProvider provider)
   {
      try {
         return SByte.Parse(byteString, NumberStyles.HexNumber);
      }   
      catch (OverflowException e) {
         throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", 
                                                   Byte.Parse(byteString, NumberStyles.HexNumber)), e);
      } 
   }

   public float ToSingle(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToSingle(SByte.Parse(byteString, NumberStyles.HexNumber));
      else
         return Convert.ToSingle(Byte.Parse(byteString, NumberStyles.HexNumber));
   }

   public string ToString(IFormatProvider provider)
   {
      return "0x" + this.byteString;
   }
   
   public object ToType(Type conversionType, IFormatProvider provider)
   {
      switch (Type.GetTypeCode(conversionType))
      {
         case TypeCode.Boolean: 
            return this.ToBoolean(null);
         case TypeCode.Byte:
            return this.ToByte(null);
         case TypeCode.Char:
            return this.ToChar(null);
         case TypeCode.DateTime:
            return this.ToDateTime(null);
         case TypeCode.Decimal:
            return this.ToDecimal(null);
         case TypeCode.Double:
            return this.ToDouble(null);
         case TypeCode.Int16:
            return this.ToInt16(null);
         case TypeCode.Int32:
            return this.ToInt32(null);
         case TypeCode.Int64:
            return this.ToInt64(null);
         case TypeCode.Object:
            if (typeof(ByteString).Equals(conversionType))
               return this;
            else
               throw new InvalidCastException(String.Format("Conversion to a {0} is not supported.", conversionType.Name));
         case TypeCode.SByte:
            return this.ToSByte(null);
         case TypeCode.Single:
            return this.ToSingle(null);
         case TypeCode.String:
            return this.ToString(null);
         case TypeCode.UInt16:
            return this.ToUInt16(null);
         case TypeCode.UInt32:
            return this.ToUInt32(null);
         case TypeCode.UInt64:
            return this.ToUInt64(null);   
         default:
            throw new InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name));   
      }
   }
   
   public UInt16 ToUInt16(IFormatProvider provider) 
   {
      if (signBit == SignBit.Negative)
         throw new OverflowException(String.Format("{0} is outside the range of the UInt16 type.", 
                                                   SByte.Parse(byteString, NumberStyles.HexNumber)));
      else
         return Convert.ToUInt16(Byte.Parse(byteString, NumberStyles.HexNumber));
   }

   public UInt32 ToUInt32(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         throw new OverflowException(String.Format("{0} is outside the range of the UInt32 type.", 
                                                   SByte.Parse(byteString, NumberStyles.HexNumber)));
      else
         return Convert.ToUInt32(Byte.Parse(byteString, NumberStyles.HexNumber));
   }
   
   public UInt64 ToUInt64(IFormatProvider provider) 
   {
      if (signBit == SignBit.Negative)
         throw new OverflowException(String.Format("{0} is outside the range of the UInt64 type.", 
                                                   SByte.Parse(byteString, NumberStyles.HexNumber)));
      else
         return Convert.ToUInt64(Byte.Parse(byteString, NumberStyles.HexNumber));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:206,代码来源:Convert.ToSByte

示例6: Main

public class Class1
{
   public static void Main()
   {
      sbyte positiveByte = 120;
      sbyte negativeByte = -101;

      ByteString positiveString = new ByteString();
      positiveString.Sign = (SignBit) Math.Sign(positiveByte);
      positiveString.Value = positiveByte.ToString("X2");
      
      ByteString negativeString = new ByteString();
      negativeString.Sign = (SignBit) Math.Sign(negativeByte);
      negativeString.Value = negativeByte.ToString("X2");
      
      try {
         Console.WriteLine("'{0}' converts to {1}.", positiveString.Value, Convert.ToSByte(positiveString));
      }
      catch (OverflowException) {
         Console.WriteLine("0x{0} is outside the range of the Byte type.", positiveString.Value);
      }

      try {
         Console.WriteLine("'{0}' converts to {1}.", negativeString.Value, Convert.ToSByte(negativeString));
      }
      catch (OverflowException) {
         Console.WriteLine("0x{0} is outside the range of the Byte type.", negativeString.Value);
      }   
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:30,代码来源:Convert.ToSByte

输出:

'78' converts to 120.
'9B' converts to -101.

示例7: foreach

ulong[] numbers = { UInt64.MinValue, 121, 340, UInt64.MaxValue };
sbyte result;

foreach (ulong number in numbers)
{
   try {
      result = Convert.ToSByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the SByte type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Convert.ToSByte

输出:

Converted the UInt64 value 0 to the SByte value 0.
Converted the UInt64 value 121 to the SByte value 121.
The UInt64 value 340 is outside the range of the SByte type.
The UInt64 value 18446744073709551615 is outside the range of the SByte type.

示例8: foreach

uint[] numbers = { UInt32.MinValue, 121, 340, UInt32.MaxValue };
sbyte result;

foreach (uint number in numbers)
{
   try {
      result = Convert.ToSByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the SByte type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Convert.ToSByte

输出:

Converted the UInt32 value 0 to the SByte value 0.
Converted the UInt32 value 121 to the SByte value 121.
The UInt32 value 340 is outside the range of the SByte type.
The UInt32 value 4294967295 is outside the range of the SByte type.

示例9: foreach

object[] values = { true, -12, 163, 935, 'x', "104", "103.0", "-1",
                    "1.00e2", "One", 1.00e2};
sbyte result;

foreach (object value in values)
{
   try {
      result = Convert.ToSByte(value);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        value.GetType().Name, value,
                        result.GetType().Name, result);
   }                     
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the SByte type.",
                        value.GetType().Name, value);
   }
   catch (FormatException) {
      Console.WriteLine("The {0} value {1} is not in a recognizable format.",
                        value.GetType().Name, value);
   }
   catch (InvalidCastException) {
      Console.WriteLine("No conversion to a Byte exists for the {0} value {1}.",
                        value.GetType().Name, value);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:25,代码来源:Convert.ToSByte

输出:

Converted the Boolean value true to the SByte value 1.
Converted the Int32 value -12 to the SByte value -12.
The Int32 value 163 is outside the range of the SByte type.
The Int32 value 935 is outside the range of the SByte type.
Converted the Char value x to the SByte value 120.
Converted the String value 104 to the SByte value 104.
The String value 103.0 is not in a recognizable format.
Converted the String value -1 to the SByte value -1.
The String value 1.00e2 is not in a recognizable format.
The String value One is not in a recognizable format.
Converted the Double value 100 to the SByte value 100.

示例10: foreach

ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue };
sbyte result;

foreach (ushort number in numbers)
{
   try {
      result = Convert.ToSByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }                     
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the SByte type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Convert.ToSByte

输出:

Converted the UInt16 value 0 to the SByte value 0.
Converted the UInt16 value 121 to the SByte value 121.
The UInt16 value 340 is outside the range of the SByte type.
The UInt16 value 65535 is outside the range of the SByte type.

示例11: foreach

int[] numbers = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue };
sbyte result;

foreach (int number in numbers)
{
   try {
      result = Convert.ToSByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the SByte type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Convert.ToSByte

输出:

The Int32 value -2147483648 is outside the range of the SByte type.
Converted the Int32 value -1 to the SByte value -1.
Converted the Int32 value 0 to the SByte value 0.
Converted the Int32 value 121 to the SByte value 121.
The Int32 value 340 is outside the range of the SByte type.
The Int32 value 2147483647 is outside the range of the SByte type.

示例12: foreach

long[] numbers = { Int64.MinValue, -1, 0, 121, 340, Int64.MaxValue };
sbyte result;
foreach (long number in numbers)
{
   try {
      result = Convert.ToSByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the SByte type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:Convert.ToSByte

输出:

The Int64 value -9223372036854775808 is outside the range of the SByte type.
Converted the Int64 value -1 to the SByte value -1.
Converted the Int64 value 0 to the SByte value 0.
Converted the Int64 value 121 to the SByte value 121.
The Int64 value 340 is outside the range of the SByte type.
The Int64 value 9223372036854775807 is outside the range of the SByte type.

示例13: foreach

byte[] numbers = { Byte.MinValue, 10, 100, Byte.MaxValue };
sbyte result;

foreach (byte number in numbers)
{
   try {
      result = Convert.ToSByte(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }                           
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the SByte type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Convert.ToSByte

输出:

Converted the Byte value 0 to the SByte value 0.
Converted the Byte value 10 to the SByte value 10.
Converted the Byte value 100 to the SByte value 100.
The Byte value 255 is outside the range of the SByte type.

示例14: foreach

char[] chars = { 'a', 'z', '\u0007', '\u0200', '\u1023' };
foreach (char ch in chars)
{
   try {
      sbyte result = Convert.ToSByte(ch);
      Console.WriteLine("{0} is converted to {1}.", ch, result);
   }   
   catch (OverflowException) {
      Console.WriteLine("Unable to convert u+{0} to a byte.",
                        Convert.ToInt16(ch).ToString("X4"));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:12,代码来源:Convert.ToSByte

输出:

a is converted to 97.
z is converted to 122.
is converted to 7.
Unable to convert u+00C8 to a byte.
Unable to convert u+03FF to a byte.

示例15:

bool falseFlag = false;
bool trueFlag = true;

Console.WriteLine("{0} converts to {1}.", falseFlag,
                  Convert.ToSByte(falseFlag));
Console.WriteLine("{0} converts to {1}.", trueFlag,
                  Convert.ToSByte(trueFlag));
开发者ID:.NET开发者,项目名称:System,代码行数:7,代码来源:Convert.ToSByte

输出:

false converts to 0.
true converts to 1.


注:本文中的System.Convert.ToSByte方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。