【问题标题】:In C# is it possible to convert an integer to an unsinged integer without a typecast? [closed]在 C# 中是否可以在没有类型转换的情况下将整数转换为无符号整数? [关闭]
【发布时间】:2018-06-09 20:37:08
【问题描述】:

是否可以在不需要显式类型转换的情况下将整数转换为无符号整数?

我有以下代码-

using System;

class MainClass
{
    public static void Main ()
    {
        int num1 = -33;
        uint num2 = Convert.ToUInt32 (num1);
        Console.WriteLine (num2);
    }
}

它给我带来了错误 -

System.OverflowException 已被抛出。

值对于 UInt32 来说太大或太小

我不想使用显式类型转换。这样可以吗?

该问题与How can I convert a signed integer into an unsigned integer? 问题不是重复的,正如我特别提到的,我想知道是否可以不使用显式类型转换?

【问题讨论】:

标签: c# exception type-conversion unsigned signed


【解决方案1】:

我想知道是否可以不进行显式类型转换?

简短的回答是:不。

我想知道函数Convert.ToUInt32()存在的原因

Answer: microsoft 的某个人发现它很有帮助并创建了它。其他人发现它有用/易于使用并使用它。我想我们不会找到实现该功能的具体原因。

有关此主题的更多信息:

This answer 表示 4 种可能性。 它们中的每一个都包含一个类型转换。我不知道将 uint 转换为 int 的另一个内置功能。 That answer 很好地解释了 uint->int 的转换及其陷阱。

(UltraShort)总结:您必须注意溢出。

在处理无符号类型时,您应该看看 checked 和 unchecked 关键字。

类型转换

根据this,TypeConversion 有 4 种方法。

  • 隐式
  • 显式
  • 使用conversion oerators 的用户定义转换。
  • 使用 helüer 类进行转换(如 Convert 类)

Convert.ToInt32()/Convert.ToUInt32()的用例

ToInt32() 的代码示例取自 msdn。

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

foreach (float value in values)
{
   try {
      result = Convert.ToInt32(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("{0} is outside the range of the Int32 type.", value);
   }   
}                                 
// The example displays the following output:
//    -3.40282346638529E+38 is outside the range of the Int32 type.
//    -13799999488 is outside the range of the Int32 type.
//    Converted the Double value -1023.29901123047 to the Int32 value -1023.
//    Converted the Double value -12.9799995422363 to the Int32 value -13.
//    Converted the Double value 0 to the Int32 value 0.
//    Converted the Double value 9.11299983940444E-16 to the Int32 value 0.
//    Converted the Double value 103.918998718262 to the Int32 value 104.
//    Converted the Double value 17834.19140625 to the Int32 value 17834.
//    3.40282346638529E+38 is outside the range of the Int32 type.

Convert 的代码示例。ToUInt(Int32) 也取自 msdn

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);
   }
}
// The example displays the following output:
//    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.

【讨论】:

    【解决方案2】:

    根据 OP 评论的要求:

    //Sample inputs
    string str = "15";
    bool b = false;
    int i = 15;
    double d = 14.5;
    object o = str;
    object oi = i;
    
    //These are not valid
    //uint ustr = (uint)str; 
    //uint ub = (uint)b;
    
    //This is valid at compile time but throw a run time exception
    //uint uo = (uint)o;
    //uint uoi = (uint)oi;
    
    //These are valid
    uint ustr = Convert.ToUInt32(str);
    uint ub = Convert.ToUInt32(b);
    uint uo = Convert.ToUInt32(o);
    uint uoi2 = Convert.ToUInt32(oi);
    
    //both ways are valid
    uint ud = (uint)d;
    uint ud2 = Convert.ToUInt32(d);
    uint ui = (uint)i;
    uint ui2 = Convert.ToUInt32(i);
    
    //Some inputs can't be converted by either way (casting may be possible but result in wrong values)
    string str2 = "Bananas";
    int i2 = -15;
    

    【讨论】:

      猜你喜欢
      • 2012-08-13
      • 2016-10-01
      • 1970-01-01
      • 2012-01-09
      • 2012-08-02
      • 2014-05-02
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多