gpt4 book ai didi

java - 如何在 Java 与 swift 中将 long 转换为 int?

转载 作者:搜寻专家 更新时间:2023-10-30 21:52:06 25 4
gpt4 key购买 nike

我尝试将这段 Java 代码转换为 swift 2.0

long checksumValue = 4253726258L;
int liCrc32 = (int)checksumValue;
System.out.println("liCrc32" + liCrc32)

LiCrc32 等于-41241038

我的swift版本

let int64: Int64 = 4253726258
let a:Int = Int(int64)

a 返回我 4253726258

我不明白为什么在 Java 中这个值是负数?

最佳答案

来自Swift documentation

Int

In most cases, you don’t need to pick a specific size of integer to use in your code. Swift provides an additional integer type, Int, which has the same size as the current platform’s native word size:

  • On a 32-bit platform, Int is the same size as Int32.

  • On a 64-bit platform, Int is the same size as Int64.

Unless you need to work with a specific size of integer, always use Int for integer values in your code. This aids code consistency and interoperability. Even on 32-bit platforms, Int can store any value between -2,147,483,648 and 2,147,483,647, and is large enough for many integer ranges.

Java's int, on the other hand, is always 4 bytes.

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, [...]

long 转换为 int 导致 narrowing primitive conversion

A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.

关于java - 如何在 Java 与 swift 中将 long 转换为 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32848490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com