开发者

64-bit unsigned to 32-bit signed

开发者 https://www.devze.com 2022-12-20 13:32 出处:网络
I need to convert Java long datatype (64-bit) data into legacy c++ app unsigned int (32-bit) datatype.

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).


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号