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


C# Convert.ToUInt32方法代码示例

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


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

示例1: foreach

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

foreach (float value in values)
{
   try {
      result = Convert.ToUInt32(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 UInt32 type.",
                        value.GetType().Name, value);
   }   
}
开发者ID:.NET开发者,项目名称:System,代码行数:17,代码来源:Convert.ToUInt32

输出:

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

示例2: Main

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

public class Class1
{
   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 numeric strings.
      string[] values = { "123456789", "+123456789", "pos 123456789", 
                          "123456789.", "123,456,789",  "4294967295", 
                          "4294967296", "-1", "neg 1" };

      foreach (string value in values)
      {
         Console.Write("{0,-20} -->", value);
         try {
            Console.WriteLine("{0,20}", Convert.ToUInt32(value, provider));
         }   
         catch (FormatException) {
            Console.WriteLine("{0,20}", "Bad Format");
         }
         catch (OverflowException) {
            Console.WriteLine("{0,20}", "Numeric Overflow");
         }   
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:34,代码来源:Convert.ToUInt32

输出:

123456789            -->           123456789
+123456789           -->          Bad Format
pos 123456789        -->           123456789
123456789.           -->          Bad Format
123,456,789          -->          Bad Format
4294967295           -->          4294967295
4294967296           -->    Numeric Overflow
-1                   -->          Bad Format
neg 1                -->    Numeric Overflow

示例3: 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 > 8)
            throw new ArgumentException("The string representation of a 32-bit integer cannot have more than 8 characters.");
         else if (! Regex.IsMatch(value, "([0-9,A-F]){1,8}", RegexOptions.IgnoreCase))
            throw new ArgumentException("The hexadecimal representation of a 32-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.ToInt32(hexString, 16))); 
      else
         try {
            return Byte.Parse(hexString, NumberStyles.HexNumber);
         }
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToUInt32(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.ToInt32(hexString, 16)));
      
      try {
         ushort codePoint = UInt16.Parse(this.hexString, NumberStyles.HexNumber);
         return Convert.ToChar(codePoint);
      }
      catch (OverflowException e) {
         throw new OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToUInt32(hexString, 16)), e);
      }      
   } 
   
   public DateTime ToDateTime(IFormatProvider provider)
   {
      throw new InvalidCastException("Hexadecimal to DateTime conversion is not supported.");
   }
   
   public decimal ToDecimal(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
      {
         int hexValue = Int32.Parse(hexString, NumberStyles.HexNumber);
         return Convert.ToDecimal(hexValue);
      }
      else
      {
         uint hexValue = UInt32.Parse(hexString, NumberStyles.HexNumber);
         return Convert.ToDecimal(hexValue);
      }
   }
   
   public double ToDouble(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToDouble(Int32.Parse(hexString, NumberStyles.HexNumber));
      else
         return Convert.ToDouble(UInt32.Parse(hexString, NumberStyles.HexNumber));
   }   
   
   public short ToInt16(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         try {
            return Convert.ToInt16(Int32.Parse(hexString, NumberStyles.HexNumber));
         }
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is out of range of the Int16 type.", Convert.ToInt32(hexString, 16)), e);
         }
      else
         try {
            return Convert.ToInt16(UInt32.Parse(hexString, NumberStyles.HexNumber));
         }
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is out of range of the Int16 type.", Convert.ToUInt32(hexString, 16)), e);
         }
   }
   
   public int ToInt32(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Int32.Parse(hexString, NumberStyles.HexNumber);
      else
         try {
            return Convert.ToInt32(UInt32.Parse(hexString, NumberStyles.HexNumber));
         }
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is out of range of the Int32 type.", Convert.ToUInt32(hexString, 16)), e);
         }   
   }
   
   public long ToInt64(IFormatProvider provider) 
   {
      if (signBit == SignBit.Negative)
         return Convert.ToInt64(Int32.Parse(hexString, NumberStyles.HexNumber));
      else
         return Int64.Parse(hexString, NumberStyles.HexNumber);
   }
   
   public sbyte ToSByte(IFormatProvider provider) 
   {
      if (signBit == SignBit.Negative)
         try {
            return Convert.ToSByte(Int32.Parse(hexString, NumberStyles.HexNumber));
         }
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", 
                                                      Int32.Parse(hexString, NumberStyles.HexNumber), e));
         }
      else
         try {
            return Convert.ToSByte(UInt32.Parse(hexString, NumberStyles.HexNumber));
         }
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", 
                                                    UInt32.Parse(hexString, NumberStyles.HexNumber)), e);
         }   
   }

   public float ToSingle(IFormatProvider provider)
   {
      if (signBit == SignBit.Negative)
         return Convert.ToSingle(Int32.Parse(hexString, NumberStyles.HexNumber));
      else
         return Convert.ToSingle(UInt32.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 ushort ToUInt16(IFormatProvider provider) 
   {
      if (signBit == SignBit.Negative)
         throw new OverflowException(String.Format("{0} is outside the range of the UInt16 type.",
                                                   Int32.Parse(hexString, NumberStyles.HexNumber)));
      else
         try {
            return Convert.ToUInt16(UInt32.Parse(hexString, NumberStyles.HexNumber));
         }   
         catch (OverflowException e) {
            throw new OverflowException(String.Format("{0} is out of range of the UInt16 type.", Convert.ToUInt32(hexString, 16)), e);
         }            
   }

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

示例4: Main

public class Example
{
   public static void Main()
   {
      uint positiveValue = 320000000;
      int negativeValue = -1;
      
      HexString positiveString = new HexString();
      positiveString.Sign = (SignBit) Math.Sign(positiveValue);
      positiveString.Value = positiveValue.ToString("X4");
      
      HexString negativeString = new HexString();
      negativeString.Sign = (SignBit) Math.Sign(negativeValue);
      negativeString.Value = negativeValue.ToString("X4");
      
      try {
         Console.WriteLine("0x{0} converts to {1}.", positiveString.Value, Convert.ToUInt32(positiveString));
      }
      catch (OverflowException) {
         Console.WriteLine("{0} is outside the range of the UInt32 type.", 
                           Int32.Parse(positiveString.Value, NumberStyles.HexNumber));
      }

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

输出:

0x1312D000 converts to 320000000.
-1 is outside the range of the UInt32 type.

示例5: Main

public class Example
{
   public static void Main()
   {
      string[] hexStrings = { "80000000", "0FFFFFFF", "F0000000", "00A3000", "D", 
                              "-13", "9AC61", "GAD", "FFFFFFFFFF" };
      
      foreach (string hexString in hexStrings)
      {
         Console.Write("{0,-12}  -->  ", hexString);
         try {
            uint number = Convert.ToUInt32(hexString, 16);
            Console.WriteLine("{0,18:N0}", number);
         }
         catch (FormatException) {
            Console.WriteLine("{0,18}", "Bad Format");
         }   
         catch (OverflowException)
         {
            Console.WriteLine("{0,18}", "Numeric Overflow");
         }   
         catch (ArgumentException) {
            Console.WriteLine("{0,18}", "Invalid in Base 16");
         }
      }                                            
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:27,代码来源:Convert.ToUInt32

输出:

80000000      -->       2,147,483,648
0FFFFFFF      -->         268,435,455
F0000000      -->       4,026,531,840
00A3000       -->             667,648
D             -->                  13
-13           -->  Invalid in Base 16
9AC61         -->             633,953
GAD           -->          Bad Format
FFFFFFFFFF    -->    Numeric Overflow

示例6:

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

示例7: if

// Create a negative hexadecimal value out of range of the UInt32 type.
int sourceNumber = Int32.MinValue;
bool isSigned = Math.Sign((int)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1;
string value = Convert.ToString(sourceNumber, 16);
UInt32 targetNumber;
try
{
   targetNumber = Convert.ToUInt32(value, 16);
   if (isSigned && ((targetNumber & 0x80000000) != 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 integer.", 
                     value);
} 
// Displays the following to the console:
//    Unable to convert '0x80000000' to an unsigned integer.
开发者ID:.NET开发者,项目名称:System,代码行数:20,代码来源:Convert.ToUInt32

示例8: foreach

ulong[] numbers = { UInt64.MinValue, 121, 340, UInt64.MaxValue };
uint result;
foreach (ulong number in numbers)
{
   try {
      result = Convert.ToUInt32(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 UInt32 type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:Convert.ToUInt32

输出:

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

示例9: foreach

ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue };
uint result;
foreach (ushort number in numbers)
{
   result = Convert.ToUInt32(number);
   Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                     number.GetType().Name, number,
                     result.GetType().Name, result);
}
开发者ID:.NET开发者,项目名称:System,代码行数:9,代码来源:Convert.ToUInt32

输出:

Converted the UInt16 value 0 to the UInt32 value 0.
Converted the UInt16 value 121 to the UInt32 value 121.
Converted the UInt16 value 340 to the UInt32 value 340.
Converted the UInt16 value 65535 to the UInt32 value 65535.

示例10: foreach

string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00",
                    " 0", "137", "1601.9", Int32.MaxValue.ToString() };
uint result;

foreach (string value in values)
{
   try {
      result = Convert.ToUInt32(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 UInt32 type.",
                        value.GetType().Name, value);
   }   
   catch (FormatException) {
      Console.WriteLine("The {0} value '{1}' is not in a recognizable format.",
                        value.GetType().Name, value);
   }   
}
开发者ID:.NET开发者,项目名称:System,代码行数:21,代码来源:Convert.ToUInt32

输出:

The String value 'One' is not in a recognizable format.
The String value '1.34e28' is not in a recognizable format.
The String value '-26.87' is not in a recognizable format.
The String value '-18' is outside the range of the UInt32 type.
The String value '-6.00' is not in a recognizable format.
Converted the String value ' 0' to the UInt32 value 0.
Converted the String value '137' to the UInt32 value 137.
The String value '1601.9' is not in a recognizable format.
Converted the String value '2147483647' to the UInt32 value 2147483647.

示例11: foreach

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

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

输出:

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

示例12: foreach

char[] chars = { 'a', 'z', '\u0007', '\u03FF',
                 '\u7FFF', '\uFFFE' };
uint result;
                        
foreach (char ch in chars)
{
   result = Convert.ToUInt32(ch);
   Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                     ch.GetType().Name, ch,
                     result.GetType().Name, result);
}
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:Convert.ToUInt32

输出:

Converted the Char value 'a' to the UInt32 value 97.
Converted the Char value 'z' to the UInt32 value 122.
Converted the Char value '' to the UInt32 value 7.
Converted the Char value '?' to the UInt32 value 1023.
Converted the Char value '?' to the UInt32 value 32767.
Converted the Char value '?' to the UInt32 value 65534.

示例13: foreach

long[] numbers = { Int64.MinValue, -1, 0, 121, 340, Int64.MaxValue };
uint result;
foreach (long number in numbers)
{
   try {
      result = Convert.ToUInt32(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 UInt32 type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:Convert.ToUInt32

输出:

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

示例14: foreach

int[] numbers = { Int32.MinValue, -1203, 0, 121, 1340, Int32.MaxValue };
uint result;
foreach (int number in numbers)
{
   try {
      result = Convert.ToUInt32(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 UInt32 type.",
                        number.GetType().Name, number);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:15,代码来源:Convert.ToUInt32

输出:

The Int32 value -2147483648 is outside the range of the UInt32 type.
The Int32 value -1203 is outside the range of the UInt32 type.
Converted the Int32 value 0 to the UInt32 value 0.
Converted the Int32 value 121 to the UInt32 value 121.
Converted the Int32 value 1340 to the UInt32 value 1340.
Converted the Int32 value 2147483647 to the UInt32 value 2147483647.

示例15: foreach

short[] numbers= { Int16.MinValue, -1, 0, 121, 340, Int16.MaxValue };
uint result;

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

输出:

The Int16 value -32768 is outside the range of the UInt32 type.
The Int16 value -1 is outside the range of the UInt32 type.
Converted the Int16 value 0 to the UInt32 value 0.
Converted the Int16 value 121 to the UInt32 value 121.
Converted the Int16 value 340 to the UInt32 value 340.
Converted the Int16 value 32767 to the UInt32 value 32767.


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