Search code examples
64-bitbit-manipulation32-bitunsignedsigned

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


Solution

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