Packing out the front of a binary number with 0s

106 Views Asked by At

I have an issue with how an interface is reading the data i send it.

what i want to do is send the interface the current time. However the interface uses two's compliment so when i do this:

Time = time.time()
timer = struct.pack('<d', Time)

The interface receieves a binary number similar to this 1010010111111000011111000010111 and since it used twos compliment it gets the time totally wrong. It gets a negative number and seems to think its 1942.

My question is how do I convert Time to binary then pack the front of the binary number with 0s so it is 8bytes long, so i can then pack it and send it to my interface.

2

There are 2 best solutions below

4
On

Like the docs say, d is for 8-byte floats. 8-byte integers use Q or q.

0
On

If, as you indicate in your comments to Ignacio's answer, you have replaced the erroneous d with q or Q, then I think you just have the endianness wrong now, because struct.pack does always pad its output to the right length. Try struct.pack(">Q", Time) instead.