2

This is for file compression. I would like to save a binary string, for example "00011110" as that actual sequence of 1s and 0s, so that when I use Powershell's "format-hex" command to view the saved file, I will see

1E

which is the hex representation of the binary number 00011110. Rather than

00 00 00 01 01 01 01 00

which is what bytearray() would give me. It seems like there should be a simple way to do this.

3
  • int("00011110", 2) will parse the string as a binary number. Commented Dec 23, 2020 at 20:33
  • Does this answer your question? How to return a number as a binary string with a set number of bits in python Commented Dec 23, 2020 at 20:35
  • not really; the problem is how to save it to file. ie. a "binary string" saved from python does not actually save in literal binary, it still uses ascii Commented Dec 23, 2020 at 20:40

1 Answer 1

3

Your problem is threefold: You must first parse the string into an integer, and then that integer has to be converted to a bytestring which can be written to a file.

file    = open(path_to_file, 'wb') # open file in binary write mode
integer = int("00011110", 2)
byte    = integer.to_bytes(1,'big') # endianness is irrelevant with a single byte
file.write(byte) # writes the single byte to the file

This should allow one to write octets in binary mode and write them directly; the file should now contain only the actual octet 00011110, or the integer 30, or the control character record separator.

Sign up to request clarification or add additional context in comments.

7 Comments

Would it be enough to use b'00011110'?
line 3 causes AttributeError: 'str' object has no attribute 'to_bytes'
@mapf No, that is a completely different bytestring that is in fact 8 bytes long. That byte string contains the ASCII encoding of 8 different characters which isn't what o.p. wants.
@aloea, that's my mistake — I just fixed it. I forgot to parse the string
@Zorf I see, thanks! I've never worked with them, I thought that's what they are used for.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.