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


C# Convert.ToUInt16方法代码示例

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


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

示例1: foreach

string[] values = { "1603", "1,603", "one", "1.6e03", "1.2e-02", 
                    "-1326", "1074122" };
ushort result;

foreach (string value in values)
{
   try {
      result = Convert.ToUInt16(value);
      Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                        value.GetType().Name, value,
                        result.GetType().Name, result);
   }                     
   catch (FormatException) {
      Console.WriteLine("The {0} value {1} is not in a recognizable format.",
                        value.GetType().Name, value);
   }                     
   catch (OverflowException) {
      Console.WriteLine("{0} is outside the range of the UInt16 type.", value);
   }   
}
开发者ID:.NET开发者,项目名称:System,代码行数:20,代码来源:Convert.ToUInt16

输出:

Converted the String value '1603' to the UInt16 value 1603.
The String value 1,603 is not in a recognizable format.
The String value one is not in a recognizable format.
The String value 1.6e03 is not in a recognizable format.
The String value 1.2e-02 is not in a recognizable format.
-1326 is outside the range of the UInt16 type.
1074122 is outside the range of the UInt16 type.

示例2: foreach

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

foreach (uint number in numbers)
{
   try {
      result = Convert.ToUInt16(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 UInt16 type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Convert.ToUInt16

输出:

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

示例3: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      string[] hexStrings = { "8000", "0FFF", "f000", "00A30", "D", "-13", 
                              "9AC61", "GAD" };
      foreach (string hexString in hexStrings)
      {
         try {
            ushort number = Convert.ToUInt16(hexString, 16);
            Console.WriteLine("Converted '{0}' to {1:N0}.", hexString, number);
         }
         catch (FormatException) {
            Console.WriteLine("'{0}' is not in the correct format for a hexadecimal number.", 
                              hexString);
         }
         catch (OverflowException) {
            Console.WriteLine("'{0}' is outside the range of an Int16.", hexString);
         }
         catch (ArgumentException) {
            Console.WriteLine("'{0}' is invalid in base 16.", hexString);
         }
      }                                            
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:28,代码来源:Convert.ToUInt16

输出:

Converted '8000' to 32,768.
Converted '0FFF' to 4,095.
Converted 'f000' to 61,440.
Converted '00A30' to 2,608.
Converted 'D' to 13.
'-13' is invalid in base 16.
'9AC61' is outside the range of an Int16.
'GAD' is not in the correct format for a hexadecimal number.

示例4:

// Create a hexadecimal value out of range of the UInt16 type.
string value = Convert.ToString(Int16.MinValue, 16);
// Convert it back to a number.
try
{
   UInt16 number = Convert.ToUInt16(value, 16);
   Console.WriteLine("0x{0} converts to {1}.", value, number);
}   
catch (OverflowException)
{
   Console.WriteLine("Unable to convert '0x{0}' to an unsigned short integer.", 
                     value);
}
开发者ID:.NET开发者,项目名称:System,代码行数:13,代码来源:Convert.ToUInt16

示例5: if

// Create a negative hexadecimal value out of range of the UInt16 type.
short sourceNumber = Int16.MinValue;
bool isSigned = Math.Sign((short)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1;
string value = Convert.ToString(sourceNumber, 16);
UInt16 targetNumber;
try
{
   targetNumber = Convert.ToUInt16(value, 16);
   if (isSigned && ((targetNumber & 0x8000) != 0))
      throw new OverflowException();
   else 
      Console.WriteLine("0x{0} converts to {1}.", value, targetNumber);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert '0x{0}' to an unsigned short integer.", value);
} 
// Displays the following to the console:
//    Unable to convert '0x8000' to an unsigned short integer.
开发者ID:.NET开发者,项目名称:System,代码行数:19,代码来源:Convert.ToUInt16

示例6: if

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

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

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

   public string Value
   { 
      set {
         if (value.Trim().Length > 4)
            throw new ArgumentException("The string representation of a 160bit integer cannot have more than four characters.");
         else if (! Regex.IsMatch(value, "([0-9,A-F]){1,4}", RegexOptions.IgnoreCase))
            throw new ArgumentException("The hexadecimal representation of a 16-bit integer contains invalid characters.");
         else
            hexString = value;
      }
      get { return hexString; }
   }
   
   // IConvertible implementations.
   public TypeCode GetTypeCode() {
      return TypeCode.Object;
   }
   
   public bool ToBoolean(IFormatProvider provider)
   {
      return signBit != SignBit.Zero;
   } 
   
   public byte ToByte(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         throw new OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToInt16(hexString, 16)));
      else
         try {
            return Convert.ToByte(UInt16.Parse(hexString, NumberStyles.HexNumber));
         }
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is out of range of the UInt16 type.", Convert.ToUInt16(hexString, 16)), e);
         }
   }
   
   public char ToChar(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative) { 
         throw new OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToInt16(hexString, 16)));
      }
      
      UInt16 codePoint = UInt16.Parse(this.hexString, NumberStyles.HexNumber);
      return Convert.ToChar(codePoint);
   } 
   
   public DateTime ToDateTime(IFormatProvider provider)
   {
      throw new InvalidCastException("Hexadecimal to DateTime conversion is not supported.");
   }
   
   public decimal ToDecimal(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative) 
      {
         short hexValue = Int16.Parse(hexString, NumberStyles.HexNumber);
         return Convert.ToDecimal(hexValue);
      }
      else 
      {
         ushort hexValue = UInt16.Parse(hexString, NumberStyles.HexNumber);
         return Convert.ToDecimal(hexValue);
      }
   }
   
   public double ToDouble(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToDouble(Int16.Parse(hexString, NumberStyles.HexNumber));
      else
         return Convert.ToDouble(UInt16.Parse(hexString, NumberStyles.HexNumber));
   }   
   
   public short ToInt16(IFormatProvider provider) 
   {
      if (signBit == SignBit.Negative)
         return Int16.Parse(hexString, NumberStyles.HexNumber);
      else
         try {
            return Convert.ToInt16(UInt16.Parse(hexString, NumberStyles.HexNumber));
         }
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is out of range of the Int16 type.", 
                                                      Convert.ToUInt16(hexString, 16)), e);
         }
   }
   
   public int ToInt32(IFormatProvider provider) 
   {
      if (signBit == SignBit.Negative)
         return Convert.ToInt32(Int16.Parse(hexString, NumberStyles.HexNumber));
      else
         return Convert.ToInt32(UInt16.Parse(hexString, NumberStyles.HexNumber));
   }
   
   public long ToInt64(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToInt64(Int16.Parse(hexString, NumberStyles.HexNumber));
      else
         return Int64.Parse(hexString, NumberStyles.HexNumber);
   }
   
   public sbyte ToSByte(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         try {
            return Convert.ToSByte(Int16.Parse(hexString, NumberStyles.HexNumber));
         }
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", 
                                                      Int16.Parse(hexString, NumberStyles.HexNumber), e));
         }
      else
         try {
            return Convert.ToSByte(UInt16.Parse(hexString, NumberStyles.HexNumber));
         }   
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", 
                                                   UInt16.Parse(hexString, NumberStyles.HexNumber)), e);
         } 
   }

   public float ToSingle(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToSingle(Int16.Parse(hexString, NumberStyles.HexNumber));
      else
         return Convert.ToSingle(UInt16.Parse(hexString, NumberStyles.HexNumber));
   }

   public string ToString(IFormatProvider provider)
   {
      return "0x" + this.hexString;
   }
   
   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(HexString).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.", 
                                                   Int16.Parse(hexString, NumberStyles.HexNumber)));
      else
         return UInt16.Parse(hexString, 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.", 
                                                   Int16.Parse(hexString, NumberStyles.HexNumber)));
      else
         return Convert.ToUInt32(hexString, 16);
   }
   
   public UInt64 ToUInt64(IFormatProvider provider) 
   {
      if (signBit == SignBit.Negative)
         throw new OverflowException(String.Format("{0} is outside the range of the UInt64 type.", 
                                                   Int64.Parse(hexString, NumberStyles.HexNumber)));
      else
         return Convert.ToUInt64(hexString, 16);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:225,代码来源:Convert.ToUInt16

示例7: Main

public class Example
{
   public static void Main()
   {
      ushort positiveValue = 32000;
      short negativeValue = -1;
      
      HexString positiveString = new HexString();
      positiveString.Sign = (SignBit) Math.Sign(positiveValue);
      positiveString.Value = positiveValue.ToString("X2");
      
      HexString negativeString = new HexString();
      negativeString.Sign = (SignBit) Math.Sign(negativeValue);
      negativeString.Value = negativeValue.ToString("X2");
      
      try {
         Console.WriteLine("0x{0} converts to {1}.", positiveString.Value, Convert.ToUInt16(positiveString));
      }
      catch (OverflowException) {
         Console.WriteLine("{0} is outside the range of the UInt16 type.", 
                           Int16.Parse(negativeString.Value, NumberStyles.HexNumber));
      }

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

输出:

0x7D00 converts to 32000.
-1 is outside the range of the UInt16 type.

示例8: Main

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

public class Example
{
   public static void Main()
   {
      // Create a NumberFormatInfo object and set several of its
      // properties that apply to numbers.
      NumberFormatInfo provider = new NumberFormatInfo();
      provider.PositiveSign = "pos ";
      provider.NegativeSign = "neg ";

      // Define an array of strings to convert to UInt16 values.
      string[] values= { "34567", "+34567", "pos 34567", "34567.", 
                         "34567.", "65535", "65535", "65535" };         

      foreach (string value in values)
      {
         Console.Write("{0,-12:}  -->  ", value);
         try {
            Console.WriteLine("{0,17}", Convert.ToUInt16(value, provider));
         }
         catch (FormatException e) {       
            Console.WriteLine("{0,17}", e.GetType().Name);
         }     
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:30,代码来源:Convert.ToUInt16

输出:

34567         -->              34567
+34567        -->    FormatException
pos 34567     -->              34567
34567.        -->    FormatException
34567.        -->    FormatException
65535         -->              65535
65535         -->              65535
65535         -->              65535

示例9: foreach

float[] numbers = { Single.MinValue, -1.38e10f, -1023.299f, -12.98f,
                   0f, 9.113e-16f, 103.919f, 17834.191f, Single.MaxValue };
ushort result;

foreach (float number in numbers)
{
   try {
      result = Convert.ToUInt16(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("{0} is outside the range of the UInt16 type.", number);
   }   
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Convert.ToUInt16

输出:

-3.402823E+38 is outside the range of the UInt16 type.
-1.38E+10 is outside the range of the UInt16 type.
-1023.299 is outside the range of the UInt16 type.
-12.98 is outside the range of the UInt16 type.
Converted the Single value '0' to the UInt16 value 0.
Converted the Single value '9.113E-16' to the UInt16 value 0.
Converted the Single value '103.919' to the UInt16 value 104.
Converted the Single value '17834.19' to the UInt16 value 17834.
3.402823E+38 is outside the range of the UInt16 type.

示例10: foreach

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

foreach (ulong number in numbers)
{
   try {
      result = Convert.ToUInt16(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 UInt16 type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Convert.ToUInt16

输出:

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

示例11: foreach

sbyte[] numbers = { SByte.MinValue, -1, 0, 10, SByte.MaxValue };
ushort result;

foreach (sbyte number in numbers)
{
   try {
      result = Convert.ToUInt16(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("{0} is outside the range of the UInt16 type.", number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:Convert.ToUInt16

输出:

-128 is outside the range of the UInt16 type.
-1 is outside the range of the UInt16 type.
Converted the SByte value '0' to the UInt16 value 0.
Converted the SByte value '10' to the UInt16 value 10.
Converted the SByte value '127' to the UInt16 value 127.

示例12: foreach

double[] numbers = { Double.MinValue, -1.38e10, -1023.299, -12.98,
                    0, 9.113e-16, 103.919, 17834.191, Double.MaxValue };
ushort result;

foreach (double number in numbers)
{
   try {
      result = Convert.ToUInt16(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("{0} is outside the range of the UInt16 type.", number);
   }   
}
开发者ID:.NET开发者,项目名称:System,代码行数:17,代码来源:Convert.ToUInt16

输出:

-1.79769313486232E+308 is outside the range of the UInt16 type.
-13800000000 is outside the range of the UInt16 type.
-1023.299 is outside the range of the UInt16 type.
-12.98 is outside the range of the UInt16 type.
Converted the Double value '0' to the UInt16 value 0.
Converted the Double value '9.113E-16' to the UInt16 value 0.
Converted the Double value '103.919' to the UInt16 value 104.
Converted the Double value '17834.191' to the UInt16 value 17834.
1.79769313486232E+308 is outside the range of the UInt16 type.

示例13: foreach

long[] numbers = { Int64.MinValue, -1, 0, 121, 340, Int64.MaxValue };
ushort result;

foreach (long number in numbers)
{
   try {
      result = Convert.ToUInt16(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 UInt16 type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:Convert.ToUInt16

输出:

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

示例14: DateTime

object[] values= { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12),
                   "104", "103.0", "-1", "1.00e2", "One", 1.00e2};
ushort result;

foreach (object value in values)
{
   try {
      result = Convert.ToUInt16(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 UInt16 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 UInt16 exists for the {0} value {1}.",
                        value.GetType().Name, value);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:25,代码来源:Convert.ToUInt16

输出:

Converted the Boolean value 'True' to the UInt16 value 1.
The Int32 value -12 is outside the range of the UInt16 type.
Converted the Int32 value '163' to the UInt16 value 163.
Converted the Int32 value '935' to the UInt16 value 935.
Converted the Char value 'x' to the UInt16 value 120.
No conversion to a UInt16 exists for the DateTime value 5/12/2009 12:00:00 AM.
Converted the String value '104' to the UInt16 value 104.
The String value 103.0 is not in a recognizable format.
The String value -1 is outside the range of the UInt16 type.
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 UInt16 value 100.

示例15: foreach

byte[] bytes = { Byte.MinValue, 14, 122, Byte.MaxValue};
ushort result;

foreach (byte byteValue in bytes)
{
   result = Convert.ToUInt16(byteValue);
   Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                     byteValue.GetType().Name, byteValue, 
                     result.GetType().Name, result);
}
开发者ID:.NET开发者,项目名称:System,代码行数:10,代码来源:Convert.ToUInt16

输出:

Converted the Byte value '0' to the UInt16 value 0.
Converted the Byte value '14' to the UInt16 value 14.
Converted the Byte value '122' to the UInt16 value 122.
Converted the Byte value '255' to the UInt16 value 255.


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