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

Lie*_*oen 93 c#

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

Sam*_*ijo 173

鉴于:

 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
Run Code Online (Sandbox Code Playgroud)

要么

int i = Convert.ToInt32(n); //same behavior as checked
Run Code Online (Sandbox Code Playgroud)

- 编辑

包括Kenan EK提到的信息

  • 请注意,如果`uint`大于`int.MaxValue`,如果使用强制转换,则会得到否定结果;如果使用`Convert.ToInt32`,则会出现异常. (4认同)
  • 这使得Convert.ToInt32成为更好的选择. (4认同)
  • @Luke - 不,不一定.在进行投射时,它取决于您的项目构建设置,以确定是选中还是未选中arithmentic是默认设置.此外,您可以使用选中和未选中的关键字在本地修改此设置. (4认同)

Ken*_* K. 12

记下已选中未选中的关键字.

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

  • 我相信默认是未经检查的.用于指出这一点的+1 - 难以调试错误的来源. (2认同)

Dee*_*101 9

假设您只想从一种类型中提取32位并将其原样转储到另一种类型中:

uint asUint = unchecked((uint)myInt);
int asInt = unchecked((int)myUint);
Run Code Online (Sandbox Code Playgroud)

目标类型将盲目地选取32位并重新解释它们.

相反,如果您更感兴趣的是将十进制/数值保持在目标类型本身的范围内:

uint asUint = checked((uint)myInt);
int asInt = checked((int)myUint);
Run Code Online (Sandbox Code Playgroud)

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

  • 将负int(例如:-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)
Run Code Online (Sandbox Code Playgroud)

uint => int

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)
------------------------------
Run Code Online (Sandbox Code Playgroud)

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));
}  
Run Code Online (Sandbox Code Playgroud)


Nik*_*nte 6

Convert.ToInt32()将uint作为值.