0

I am trying to implement DES algorithm in Python. After the encryption phase I got bit string as "0010100100110100011001010011011101110111011110011000100010100011". Now I want to convert this bit string to bytes and write the data into the file. But the problem, is it contains non-readable characters. When I try to convert this into string, I got )4e7wy£ and for doing so, I am using

def bits2String(b):
            return ''.join(chr(int(''.join(x), 2)) for x in zip(*[iter(b)]*8))

And further when I convert string to bytes, it adds extra characters which I don't want. So, how I can perform above task so that )4e7wy£can be as it is written in file in binary mode.

Raj
  • 2,997
  • 2
  • 12
  • 30
  • Does [this](https://stackoverflow.com/questions/10237926/convert-string-to-list-of-bits-and-viceversa) help? – Badgy Apr 08 '20 at 14:37
  • Encode your file in `utf-8`? Your function returns the string just fine. – r.ook Apr 08 '20 at 14:59
  • Actually, it contains some non utf-8 characters. When I typed that in question i.e. )4e7wy£ they got changed to utf-8 only @r.ook – Raj Apr 08 '20 at 15:05
  • I guess the answer is find out the proper encoding (`utf-16`?) and use that for your text output. With your example however I am still able to save as a `utf-8` text file. – r.ook Apr 08 '20 at 15:48

0 Answers0