英文:
Given an integer in python return bytes 2-7 and translating java code
问题
# Python 3 code equivalent to the provided Java code
def get_timestamp(tracking_id):
return tracking_id & 0xFFFFFFFFFFFF
def get_counter(tracking_id):
return (tracking_id >> 48) & 0xFFFF
# Given tracking ID
tracking_id = 24305955239911
# Getting timestamp and counter
timestamp = get_timestamp(tracking_id)
counter = get_counter(tracking_id)
print("Timestamp:", timestamp)
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:
bytes 0-1 = internal tracking number
bytes 2-7 = Timestamp in nanoseconds from midnight
The java code for doing what is need is here:
public class TrackingID {
/**
* Returns the timestamp from TrackingId.
* @param trackingId - trackingId
* @return - java.lang.Long timestamp in nanoseconds from midnight
*/
public static long getTimestamp( long trackingId ) {
return trackingId & 0xffffffffffffl;
}
/**
* Returns the counter from TrackingId.
* @param trackingId - trackingId
* @return - java.lang.Short counter
*/
public static short getCounter( long trackingId ) {
return (short)( trackingId >> 48 );
}
}
How do I do that in python3? to get the timestamp and the counter?
Thanks
答案1
得分: 2
我只能假设你在处理Java常量0xffffffffffffl
,你可以将其翻译为 int('ffffffffffff', 16)
。类似这样:
class tracking_id:
@staticmethod
def get_timestamp(v):
return v & int('ffffffffffff', 16)
@staticmethod
def get_counter(v):
return v >> 48
if __name__ == "__main__":
print(tracking_id.get_timestamp(24305955239911))
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,
class tracking_id:
@staticmethod
def get_timestamp(v):
return v & int('ffffffffffff', 16)
@staticmethod
def get_counter(v):
return v >> 48
if __name__ == "__main__":
print(tracking_id.get_timestamp(24305955239911))
print(tracking_id.get_counter(24305955239911))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论