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.

4
  • Does this help? Commented Apr 8, 2020 at 14:37
  • Encode your file in utf-8? Your function returns the string just fine. Commented Apr 8, 2020 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 Commented Apr 8, 2020 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. Commented Apr 8, 2020 at 15:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.