Manipulate bytes to binary levels in python

I am trying to start learning about writing encryption algorithms, therefore, using python, I am trying to manipulate data to a binary level, so I can add bits to the end of the data, and also manipulate to obscure the data.

I am not new to programming. I'm actually a programmer, but I'm relatively new to python, so I'm struggling a bit.

can someone show me the best way to manipulate a string in python to the binary level (or recommend how I should approach this). I examined a number of issues:

Convert string to binary in python

Python binary data manipulation

Convert binary to ASCII and vice versa

But all this is not what I am looking for, and I do not know enough python to be able to choose what I need. can someone please help me with the details (if you use a function, please explain what it is for me, for example ord ())

+4
source share
2 answers

Take a look at the bitstring module , which is designed to simplify the work with binary manipulations.

from bitstring import BitArray
a = BitArray('0xfeed')    # 16 bits from hex
a += '0b001'              # Add three binary bits
a.replace('0b111', '0b0') # Search and replace
a.count(1)                # Count one bits

It has a complete guide and many examples.

+2
source

bitarray allows you to treat bit sequences as normal Python sequences and work with them as binary values.

>>> bitarray.bitarray('0101') | bitarray.bitarray('1010')
bitarray('1111')
+2

Source: https://habr.com/ru/post/1608968/


All Articles