【问题标题】:How do I convert uint to int in C#?如何在 C# 中将 uint 转换为 int?
【发布时间】:2019-08-09 22:51:49
【问题描述】:

如何在 C# 中将 uint 转换为 int?

【问题讨论】:

  • 请注意,如果这样做,可能会溢出 int 的值。
  • 是的,如果 uint 的值大于 Int32.MaxValue(恰好是 2,147,483,647),您必须确保通过将对象置于可接受的状态来优雅地处理异常
  • uint 转换为long 会更安全,因为long 可以包含所有uint 值,而int 不能(as already mentioned

标签: c#


【解决方案1】:

给定:

 uint n = 3;

int i = checked((int)n); //throws OverflowException if n > Int32.MaxValue
int i = unchecked((int)n); //converts the bits only 
                           //i will be negative if n > Int32.MaxValue

int i = (int)n; //same behavior as unchecked

int i = Convert.ToInt32(n); //same behavior as checked

--编辑

Kenan E. K.提到的包含信息

【讨论】:

  • 请注意,如果uint 大于int.MaxValue,则使用强制转换会得到否定结果,如果使用Convert.ToInt32,则会得到异常。
  • 这使得 Convert.ToInt32 成为 imo 更好的选择。
  • @Luke - 不,不一定。投射时,它取决于您的项目构建设置,是否选中或未选中算术是默认值。此外,您可以使用选中和未选中的关键字在本地进行修改。
  • @Greg:没错,但默认的开箱即用设置未选中。
  • @GregBeech 这种行为是否可移植?在未经检查的情况下??
【解决方案2】:

假设您想简单地从一种类型中提取 32 位并将它们原样转储到另一种类型中:

uint asUint = unchecked((uint)myInt);
int asInt = unchecked((int)myUint);

目标类型会盲目地选择 32 位并重新解释它们。

相反,如果您更希望将小数/数值保持在目标类型本身的范围内:

uint asUint = checked((uint)myInt);
int asInt = checked((int)myUint);

在这种情况下,如果出现以下情况,您将获得溢出异常:

  • 将负整数(例如:-1)转换为 uint
  • 将 2,147,483,648 和 4,294,967,295 之间的正 uint 转换为 int

在我们的例子中,我们希望unchecked 解决方案保持原样保留 32 位,所以这里有一些示例:

示例

int => uint

int....: 0000000000 (00-00-00-00)
asUint.: 0000000000 (00-00-00-00)
------------------------------
int....: 0000000001 (01-00-00-00)
asUint.: 0000000001 (01-00-00-00)
------------------------------
int....: -0000000001 (FF-FF-FF-FF)
asUint.: 4294967295 (FF-FF-FF-FF)
------------------------------
int....: 2147483647 (FF-FF-FF-7F)
asUint.: 2147483647 (FF-FF-FF-7F)
------------------------------
int....: -2147483648 (00-00-00-80)
asUint.: 2147483648 (00-00-00-80)

uint => 整数

uint...: 0000000000 (00-00-00-00)
asInt..: 0000000000 (00-00-00-00)
------------------------------
uint...: 0000000001 (01-00-00-00)
asInt..: 0000000001 (01-00-00-00)
------------------------------
uint...: 2147483647 (FF-FF-FF-7F)
asInt..: 2147483647 (FF-FF-FF-7F)
------------------------------
uint...: 4294967295 (FF-FF-FF-FF)
asInt..: -0000000001 (FF-FF-FF-FF)
------------------------------

代码

int[] testInts = { 0, 1, -1, int.MaxValue, int.MinValue };
uint[] testUints = { uint.MinValue, 1, uint.MaxValue / 2, uint.MaxValue };

foreach (var Int in testInts)
{
    uint asUint = unchecked((uint)Int);
    Console.WriteLine("int....: {0:D10} ({1})", Int, BitConverter.ToString(BitConverter.GetBytes(Int)));
    Console.WriteLine("asUint.: {0:D10} ({1})", asUint, BitConverter.ToString(BitConverter.GetBytes(asUint)));
    Console.WriteLine(new string('-',30));
}
Console.WriteLine(new string('=', 30));
foreach (var Uint in testUints)
{
    int asInt = unchecked((int)Uint);
    Console.WriteLine("uint...: {0:D10} ({1})", Uint, BitConverter.ToString(BitConverter.GetBytes(Uint)));
    Console.WriteLine("asInt..: {0:D10} ({1})", asInt, BitConverter.ToString(BitConverter.GetBytes(asInt)));
    Console.WriteLine(new string('-', 30));
}  

【讨论】:

    【解决方案3】:

    注意checkedunchecked 关键字。

    如果您希望将结果截断为 int 或者如果结果不适合有符号的 32 位则引发异常,这很重要。默认未选中。

    【讨论】:

    • 我相信默认是未选中的。 +1 指出这一点 - 难以调试错误的根源。
    • 你是对的,我的错,谢谢! -"此选项的默认值为 /checked-"
    • 未选中不会“截断”。
    【解决方案4】:

    Convert.ToInt32()uint 作为一个值。

    【讨论】:

      【解决方案5】:
      uint i = 10;
      int j = (int)i;
      

      int k = Convert.ToInt32(i)
      

      【讨论】:

        【解决方案6】:

        假设uint中包含的值可以用一个int来表示,那么就这么简单:

        int val = (int) uval;

        【讨论】:

          【解决方案7】:

          我会说使用 tryParse,如果 uint 对于 int 来说太大,它将返回“false”。
          不要忘记,一个 uint 可以比一个 int 大得多,只要你 > 0

          【讨论】:

            【解决方案8】:
            int intNumber = (int)uintNumber;
            

            根据您期望的值类型,您可能需要在进行转换之前检查 uintNumber 的大小。一个 int 的最大值约为 0.5 个 uint。

            【讨论】:

              猜你喜欢
              • 2012-08-27
              • 2020-01-25
              • 2014-07-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-03-05
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多