C# 我可以将 long 转换为 int 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/858904/
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
Can I convert long to int?
提问by inspite
I want to convert long
to int
.
我想转换long
为int
.
If the value of long
> int.MaxValue
, I am happy to let it wrap around.
如果long
>的值int.MaxValue
,我很乐意让它环绕。
What is the best way?
什么是最好的方法?
采纳答案by Mehrdad Afshari
Just do (int)myLongValue
. It'll do exactly what you want (discarding MSBs and taking LSBs) in unchecked
context (which is the compiler default). It'll throw OverflowException
in checked
context if the value doesn't fit in an int
:
就做(int)myLongValue
。它会在unchecked
上下文(这是编译器的默认设置)中完全按照您的要求执行(丢弃 MSB 并采用 LSB )。它会扔OverflowException
在checked
上下文中,如果值不适合的int
:
int myIntValue = unchecked((int)myLongValue);
回答by Max Schmeling
Convert.ToInt32(myValue);
Though I don't know what it will do when it's greater than int.MaxValue.
虽然我不知道当它大于 int.MaxValue 时它会做什么。
回答by realbart
Sometimes you're not actually interested in the actual value, but in its usage as checksum/hashcode. In this case, the built-in method GetHashCode()
is a good choice:
有时您实际上并不对实际值感兴趣,而是对其作为checksum/hashcode 的用法感兴趣。在这种情况下,内置方法GetHashCode()
是一个不错的选择:
int checkSumAsInt32 = checkSumAsIn64.GetHashCode();
回答by Mashake
The safe and fastest way is to use Bit Masking before cast...
安全和最快的方法是在投射之前使用位掩码...
int MyInt = (int) ( MyLong & 0xFFFFFFFF )
The Bit Mask ( 0xFFFFFFFF
) value will depend on the size of Int because Int size is dependent on machine.
位掩码 ( 0xFFFFFFFF
) 值将取决于 Int 的大小,因为 Int 大小取决于机器。
回答by Rémy Esmery
Wouldn't
不会
(int) Math.Min(Int32.MaxValue, longValue)
be the correct way, mathematically speaking?
从数学上讲,是正确的方法吗?
回答by nzrytmn
It can convert by
它可以通过转换
Convert.ToInt32 method
Convert.ToInt32 方法
But it will throw an OverflowException if it the value is outside range of the Int32 Type. A basic test will show us how it works:
但是如果它的值超出 Int32 类型的范围,它将抛出一个溢出异常。一个基本的测试将向我们展示它是如何工作的:
long[] numbers = { Int64.MinValue, -1, 0, 121, 340, Int64.MaxValue };
int result;
foreach (long number in numbers)
{
try {
result = Convert.ToInt32(number);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
number.GetType().Name, number,
result.GetType().Name, result);
}
catch (OverflowException) {
Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.",
number.GetType().Name, number);
}
}
// The example displays the following output:
// The Int64 value -9223372036854775808 is outside the range of the Int32 type.
// Converted the Int64 value -1 to the Int32 value -1.
// Converted the Int64 value 0 to the Int32 value 0.
// Converted the Int64 value 121 to the Int32 value 121.
// Converted the Int64 value 340 to the Int32 value 340.
// The Int64 value 9223372036854775807 is outside the range of the Int32 type.
Herethere is a longer explanation.
这里有一个更长的解释。
回答by Clement
The following solution will truncate to int.MinValue/int.MaxValue if the value is out of Integer bounds.
如果值超出整数范围,以下解决方案将截断为 int.MinValue/int.MaxValue。
myLong < int.MinValue ? int.MinValue : (myLong > int.MaxValue ? int.MaxValue : (int)myLong)
回答by Andre
A possible way is to use the modulo operator to only let the values stay in the int32 range, and then cast it to int.
一种可能的方法是使用模运算符只让值停留在 int32 范围内,然后将其强制转换为 int。
var intValue= (int)(longValue % Int32.MaxValue);