64 位无符号到 32 位有符号

发布于 2024-08-21 07:28:54 字数 286 浏览 5 评论 0原文

我需要将 Java long 数据类型(64 位)数据转换为旧版 c++ 应用程序 unsigned int(32 位)数据类型。

不用担心数据丢失,因为数据是 Linux 时间戳,这需要很长时间才能达到 unsigned int 限制。

知道对这些数字应用什么转换吗?

提前致谢!

PS - 数据类型示例:

Java - 1266336527340

C++ - 1266336583

它们都产生相同的日期和大约相同的时间(+/-一分钟)。

I need to convert Java long datatype (64-bit) data into legacy c++ app unsigned int (32-bit) datatype.

No worries about the data loss as the data is Linux timestamp, which would take aeons to hit unsigned int limit.

Any idea what transformation to apply to these numbers?

Thanks in advance!

P.S. - data-types example:

Java - 1266336527340

C++ - 1266336583

They both produce same date, and about same time (+/- a minute).

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

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

发布评论

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

评论(2

染年凉城似染瑾 2024-08-28 07:28:54

Java 的 Date.getTime 返回距纪元的毫秒数,而 C++ 代码期望距纪元的数,因此您需要除以 1000 然后截断:

int timestampAsInt = (int)(timestampAsLong / 1000);

Java 只有有符号整数而不是无符号整数,但这应该可以工作。

Java's Date.getTime returns the number of milliseconds from the epoch, whereas the C++ code expects the number of seconds from the epoch, so you need to divide by 1000 then truncate:

int timestampAsInt = (int)(timestampAsLong / 1000);

Java has only a signed integer not unsigned, but this should work.

旧时模样 2024-08-28 07:28:54
long javaTime = someDate.getTime();
int cTime = (int)((javaTime + 500) / 1000);

我更喜欢四舍五入而不是截断,但您必须决定哪一个最适合您的业务规则。

long javaTime = someDate.getTime();
int cTime = (int)((javaTime + 500) / 1000);

I prefer rounding over truncation, but you will have to decide which is right for your business rules.

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