Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Binary file
#11
(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'
Reply
#12
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,)
Reply
#13
Brilliant, thank you to both.
Not sure who to mark as answer to this.
Because both provided answers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I read and write a binary file in Python? blackears 6 6,016 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Hashing an address for binary file Python_help 8 2,567 Nov-04-2021, 06:23 AM
Last Post: ndc85430
  Read/Write binary file deanhystad 3 3,094 Feb-01-2021, 10:29 AM
Last Post: Larz60+
  Binary File Error SalsaBeanDip 1 1,724 Oct-12-2020, 09:13 PM
Last Post: bowlofred
  Convert file of hex strings to binary file medatib531 4 13,573 Oct-09-2020, 05:42 PM
Last Post: DeaD_EyE
  Binary File Read Aussie 6 8,226 Sep-03-2020, 03:57 AM
Last Post: deanhystad
  python read binary file Pyguys 4 3,792 Jul-13-2020, 02:34 AM
Last Post: Pyguys
  Failure in writing binary text to file Gigux 7 3,717 Jul-04-2020, 08:41 AM
Last Post: Gigux
  search binary file and list all founded keyword offset Pyguys 4 2,699 Mar-17-2020, 06:46 AM
Last Post: Pyguys
  hex file to binary or pcap to binary baran01 1 5,630 Dec-11-2019, 10:19 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020