SyBer
SyBer

Reputation: 5597

64-bit unsigned to 32-bit signed

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

Upvotes: 1

Views: 1989

Answers (2)

Frank Krueger
Frank Krueger

Reputation: 71013

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.

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838666

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.

Upvotes: 3

Related Questions