如何在 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
How do I convert uint to int in 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 uint
as 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.
回答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 unchecked
solution 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));
}