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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1131843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:57:46  来源:igfitidea点击:

How do I convert uint to int in C#?

c#

提问by Lieven Cardoen

How do I convert uint to int in C#?

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

采纳答案by Samuel Carrijo

Given:

鉴于:

 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

or

或者

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

--EDIT

- 编辑

Included info as mentioned by Kenan E. K.

Kenan EK提到的包含信息

回答by NikolaiDante

Convert.ToInt32()takes uintas a value.

Convert.ToInt32()uint作为值。

回答by Julian

Assuming that the value contained in the uint can be represented in an int, then it is as simple as:

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

int val = (int) uval;

int val = (int) uval;

回答by Jesse Vogt

int intNumber = (int)uintNumber;

Depending on what kind of values you are expecting, you may want to check how big uintNumber is before doing the conversion. An int has a max value of about .5 of a uint.

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

回答by Chris Mullins

uint i = 10;
int j = (int)i;

or

或者

int k = Convert.ToInt32(i)

回答by Kenan E. K.

Take note of the checkedand uncheckedkeywords.

记下checkedunchecked关键字。

It matters if you want the result truncated to the int or an exception raised if the result doesnt fit in signed 32 bits. The default is unchecked.

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

回答by BlueCacti

I would say using tryParse, it'll return 'false' if the uint is to big for an int.
Don't forget that a uint can go much bigger than a int, as long as you going > 0

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

回答by DeepSpace101

Assuming you want to simply lift the 32bits from one type and dump them as-is into the other type:

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

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

The destination type will blindly pick the 32 bits and reinterpret them.

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

Conversely if you're more interested in keeping the decimal/numerical values within the range of the destination type itself:

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

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

In this case, you'll get overflow exceptions if:

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

  • casting a negative int (eg: -1) to an uint
  • casting a positive uint between 2,147,483,648 and 4,294,967,295 to an int
  • 将负整数(例如:-1)转换为 uint
  • 将 2,147,483,648 和 4,294,967,295 之间的正 uint 转换为 int

In our case, we wanted the uncheckedsolution to preserve the 32bits as-is, so here are some examples:

在我们的例子中,我们希望unchecked解决方案能够按原样保留 32 位,因此这里有一些示例:

Examples

例子

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 => int

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)
------------------------------

Code

代码

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));
}