我可以将 long 转换为 int 吗?

发布于 2024-07-20 05:59:05 字数 151 浏览 4 评论 0原文

我想将 long 转换为 int

如果long的值> > int.MaxValue,我很高兴让它环绕起来。

什么是最好的方法?

I want to convert long to int.

If the value of long > int.MaxValue, I am happy to let it wrap around.

What is the best way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

献世佛 2024-07-27 05:59:05

只需执行 (int)myLongValue 即可。 它会在 unchecked 上下文(这是编译器默认值)中完全执行您想要的操作(丢弃 MSB 并获取 LSB)。 如果该值不适合 int,它将在 checked 上下文中抛出 OverflowException

int myIntValue = unchecked((int)myLongValue);

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 myIntValue = unchecked((int)myLongValue);
不奢求什么 2024-07-27 05:59:05
Convert.ToInt32(myValue);

虽然我不知道当它大于 int.MaxValue 时它会做什么。

Convert.ToInt32(myValue);

Though I don't know what it will do when it's greater than int.MaxValue.

鸩远一方 2024-07-27 05:59:05

有时您实际上并不对实际值感兴趣,而是对它作为校验和/哈希码的用法感兴趣。 在这种情况下,内置方法 GetHashCode() 是一个不错的选择:

int checkSumAsInt32 = checkSumAsIn64.GetHashCode();

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:

int checkSumAsInt32 = checkSumAsIn64.GetHashCode();
沉睡月亮 2024-07-27 05:59:05

安全且最快的方法是在转换之前使用位掩码...

int MyInt = (int) ( MyLong & 0xFFFFFFFF )

位掩码(0xFFFFFFFF)值将取决于 Int 的大小,因为 Int 大小取决于机器。

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.

暗地喜欢 2024-07-27 05:59:05

一种可能的方法是使用模运算符仅让值保持在 int32 范围内,然后将其转换为 int。

var intValue= (int)(longValue % Int32.MaxValue);

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.

var intValue= (int)(longValue % Int32.MaxValue);
肩上的翅膀 2024-07-27 05:59:05

如果值超出整数范围,以下解决方案将截断为 int.MinValue/int.MaxValue。

myLong < int.MinValue ? int.MinValue : (myLong > int.MaxValue ? int.MaxValue : (int)myLong)

The following solution will truncate to int.MinValue/int.MaxValue if the value is out of Integer bounds.

myLong < int.MinValue ? int.MinValue : (myLong > int.MaxValue ? int.MaxValue : (int)myLong)
不离久伴 2024-07-27 05:59:05

从数学角度来说,这不是

(int) Math.Min(Int32.MaxValue, longValue)

正确的方法吗?

Wouldn't

(int) Math.Min(Int32.MaxValue, longValue)

be the correct way, mathematically speaking?

祁梦 2024-07-27 05:59:05

它可以通过转换

Convert.ToInt32 方法

但如果值超出 Int32 类型的范围,则会抛出 OverflowException。
基本测试将向我们展示它是如何工作的:

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.

这里有一个更长的解释。

It can convert by

Convert.ToInt32 method

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:

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.

Here there is a longer explanation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文