简体   繁体   English

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

[英]How do I convert uint to int in C#?

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

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

Take note of the checked and unchecked keywords. 记下已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. 如果您希望将结果截断为int,或者如果结果不符合有符号的32位,则会引发异常,这很重要。 The default is unchecked. 默认为未选中。

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 将负int(例如:-1)转换为uint
  • casting a positive uint between 2,147,483,648 and 4,294,967,295 to an int 在2,147,483,648和4,294,967,295之间投入一个正的uint到int

In our case, we wanted the unchecked solution to preserve the 32bits as-is, so here are some examples: 在我们的例子中,我们希望unchecked解决方案按原样保留32位,所以这里有一些例子:

Examples 例子

int => uint 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));
}  

Convert.ToInt32()uint作为值。

I would say using tryParse, it'll return 'false' if the uint is to big for an int. 我会说使用tryParse,如果uint对于int来说很大,那么它将返回'false'。
Don't forget that a uint can go much bigger than a int, as long as you going > 0 不要忘记,只要你> 0,uint就可以比int大得多

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

int val = (int) uval;

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

or 要么

int k = Convert.ToInt32(i)
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. 根据您期望的值,您可能需要在进行转换之前检查uintNumber的大小。 An int has a max value of about .5 of a uint. int的最大值约为.5的uint。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM