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


99

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


5
请注意,如果这样做,可能会使int的值溢出。
Powerlord

1
是的,如果uint的值大于Int32.MaxValue(恰好是2,147,483,647),则必须确保通过将对象置于可接受的状态来妥善处理该异常
Michael Meadows,2009年

这将是更安全的转换uintlong一个long可以包含所有uint在那里作为一个值int不能(前面已经提到
利亚姆

Answers:


186

鉴于:

 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 EK提到的信息


4
请注意,如果uint大于,则使用强制转换int.MaxValue会产生负数结果;如果使用,则会产生异常Convert.ToInt32
路加福音

4
这使得Convert.ToInt32成为更好的imo选择。
mmcdole 2009年

4
@Luke-不,不一定。强制转换时,默认设置为选中还是未选中,取决于您的项目构建设置。此外,您可以使用选中和未选中关键字在本地进行修改。
格雷格·比奇

@Greg:是的,但是默认的开箱即用设置是未选中的。
路加福音

@GregBeech此行为可移植吗?如果采用未经检查的方法?

12

注意checkedunchecked关键字。

重要的是,您希望将结果截断为int还是引发异常(如果结果不适合带符号的32位)。默认为未选中。


2
我相信默认值为未选中。+1指出这一点-难以调试的错误源。

您说得对,我的不好,谢谢!-“此选项的默认值为/ checked-”
Kenan EK,2009年

未选中不会“截断”。
fractor

9

假设您只想从一种类型提升32位,然后将它们原样转储到另一种类型中:

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

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

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

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

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

  • 将负整数(例如-1)强制转换为整数
  • 将一个介于2,147,483,648和4,294,967,295之间的正整数转换为一个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));
}  


0

假设uint中包含的值可以用int表示,则它很简单:

int val = (int) uval;


0

我会说使用tryParse,如果uint对于int很大,它将返回“ false”。
只要您> 0,别忘了uint可以比int大得多



-1
int intNumber = (int)uintNumber;

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.