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

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

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

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:

确定