Reputation: 67
from struct import *
longValue = 1447460000
print struct.pack('!q',longValue)
The longValue is actually signifies time in epoch as the main idea is to convert the epoch time which is a long, into bytes. So I came across struct module and this seems quite helpful too.
The output that I get is:-
'\x00\x00\x00\x00VF|\xa0'
Now, I want to make sure that this result is correct, so I want to convert these bytes back to the long.
This is what I am trying:-
struct.unpack("<L", "\x00\x00\x00\x00VF|\xa0")
But this gives me error:-
struct.error: unpack requires a string argument of length 4
Any help?Thanks
Upvotes: 2
Views: 570
Reputation: 879083
Use the same struct format, !q
, to unpack as you did to pack:
In [6]: import struct
In [7]: longValue = 1447460000
In [8]: struct.pack('!q',longValue)
Out[8]: '\x00\x00\x00\x00VF|\xa0'
In [9]: struct.unpack('!q', struct.pack('!q',longValue))
Out[9]: (1447460000,)
q
is for 8-byte long ints, while L
is for 4-byte unsigned longs. That is why you got the error
struct.error: unpack requires a string argument of length 4
Upvotes: 2