Python Forum
Binary file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Binary file (/thread-29333.html)

Pages: 1 2


RE: Binary file - bowlofred - Aug-31-2020

(Aug-31-2020, 04:46 AM)Aussie Wrote: Hi, how do I convert byte into a binary string or int32 of same format?
01011111000110000110010100111010
Thanks,

The previous one showed how to print it as an int32. Just use bin() to print it in binary format. I don't know what "int32 of the same format" means.


>>> struct.unpack('>i', byte)[0]      # as int
1121643973
>>> bin(struct.unpack('>i', byte)[0]) # as binary
'0b1000010110110101110110111000101'
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(struct.unpack('>i', byte)[0])) # as time
'2005-07-17 16:46:13'



RE: Binary file - Gribouillis - Aug-31-2020

01011111 00011000 01100101 00111010 is 95, 24, 101, 58, that is to say b'_\x18e:'. The unpack method works very well
>>> struct.unpack('>I', b'_\x18e:')
(1595434298,)



RE: Binary file - Aussie - Aug-31-2020

Brilliant, thank you to both.
Not sure who to mark as answer to this.
Because both provided answers.