Given an integer in python return bytes 2-7 and translating java code

huangapple go评论105阅读模式
英文:

Given an integer in python return bytes 2-7 and translating java code

问题

  1. # Python 3 code equivalent to the provided Java code
  2. def get_timestamp(tracking_id):
  3. return tracking_id & 0xFFFFFFFFFFFF
  4. def get_counter(tracking_id):
  5. return (tracking_id >> 48) & 0xFFFF
  6. # Given tracking ID
  7. tracking_id = 24305955239911
  8. # Getting timestamp and counter
  9. timestamp = get_timestamp(tracking_id)
  10. counter = get_counter(tracking_id)
  11. print("Timestamp:", timestamp)
  12. print("Counter:", counter)

请注意,上述Python代码与提供的Java代码等效,用于从给定的tracking ID中提取时间戳和计数器信息。将提供的Java代码转换为Python代码,然后使用给定的tracking ID调用相应的函数以获取时间戳和计数器。

英文:

I am not a java dev nor a guru with dealing with bytes.

I am given this number:

trackingID = 24305955239911

I need to do the below:

  1. bytes 0-1 = internal tracking number
  2. bytes 2-7 = Timestamp in nanoseconds from midnight

The java code for doing what is need is here:

  1. public class TrackingID {
  2. /**
  3. * Returns the timestamp from TrackingId.
  4. * @param trackingId - trackingId
  5. * @return - java.lang.Long timestamp in nanoseconds from midnight
  6. */
  7. public static long getTimestamp( long trackingId ) {
  8. return trackingId & 0xffffffffffffl;
  9. }
  10. /**
  11. * Returns the counter from TrackingId.
  12. * @param trackingId - trackingId
  13. * @return - java.lang.Short counter
  14. */
  15. public static short getCounter( long trackingId ) {
  16. return (short)( trackingId >> 48 );
  17. }
  18. }

How do I do that in python3? to get the timestamp and the counter?

Thanks

答案1

得分: 2

我只能假设你在处理Java常量0xffffffffffffl,你可以将其翻译为 int('ffffffffffff', 16)。类似这样:

  1. class tracking_id:
  2. @staticmethod
  3. def get_timestamp(v):
  4. return v & int('ffffffffffff', 16)
  5. @staticmethod
  6. def get_counter(v):
  7. return v >> 48
  8. if __name__ == "__main__":
  9. print(tracking_id.get_timestamp(24305955239911))
  10. print(tracking_id.get_counter(24305955239911))
英文:

I can only assume you're struggling with the Java constant 0xffffffffffffl, you could translate that to int('ffffffffffff', 16). Something like,

  1. class tracking_id:
  2. @staticmethod
  3. def get_timestamp(v):
  4. return v & int('ffffffffffff', 16)
  5. @staticmethod
  6. def get_counter(v):
  7. return v >> 48
  8. if __name__ == "__main__":
  9. print(tracking_id.get_timestamp(24305955239911))
  10. print(tracking_id.get_counter(24305955239911))

huangapple
  • 本文由 发表于 2020年9月15日 09:44:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63893899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定