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.

aloea
  • 191
  • 1
  • 12
  • `int("00011110", 2)` will parse the string as a binary number. – Barmar Dec 23 '20 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](https://stackoverflow.com/questions/13599638/how-to-return-a-number-as-a-binary-string-with-a-set-number-of-bits-in-python) – quamrana Dec 23 '20 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 – aloea Dec 23 '20 at 20:40

1 Answers1

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.

Zorf
  • 6,334
  • 2
  • 29
  • 24
  • Would it be enough to use `b'00011110'`? – mapf Dec 23 '20 at 20:58
  • line 3 causes AttributeError: 'str' object has no attribute 'to_bytes' – aloea Dec 23 '20 at 21:03
  • @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. – Zorf Dec 23 '20 at 21:03
  • @aloea, that's my mistake — I just fixed it. I forgot to parse the string – Zorf Dec 23 '20 at 21:04
  • @Zorf I see, thanks! I've never worked with them, I thought that's what they are used for. – mapf Dec 23 '20 at 21:07
  • this is great, do you think it would work with longer bit strings or will i have to iterate through them saving 8 at a time – aloea Dec 23 '20 at 21:09
  • @aloea, one may edit the integer in the `to_bytes` part as one pleases to work with integers that consume more than one byte so that it returns byte strings of more than one byte, but be advised that at that point endianness becomes a factor — though you probably want big endian. – Zorf Dec 23 '20 at 22:22