3

I've got a little problem here. I'm converting binary to ascii, in order to compress data. All seems to work fine, but when I convert '11011011' to ascii and try to write it into file, I keep getting error

UnicodeEncodeError: 'charmap' codec can't encode character '\xdb' in position 0: character maps to

Here's my code:

 byte = ""
 handleR = open(self.getInput())
 handleW = open(self.getOutput(), 'w')
 file = handleR.readlines()
 for line in file:
 for a in range(0, len(line)):
 chunk = result[ord(line[a])]
 for b in chunk:
 if (len(byte) < 8):
 byte+=str(chunk[b])
 else:
 char = chr(eval('0b'+byte))
 print(byte, char)
 handleW.write(char)
 byte = ""
 handleR.close()
 handleW.close()

Any help appreciated,

Thank You

asked Jun 25, 2010 at 5:18
1
  • When I read "binary to ASCII" I thought you were going the other way around, i.e. '\xdb' to '11011011' :-) Commented Jun 25, 2010 at 5:28

1 Answer 1

2

I think you want:

handleR = open(self.getInput(), 'rb')
handleW = open(self.getOutput(), 'wb')

That will ensure you're reading and writing byte streams. Also, you can parse binary strings without eval:

char = chr(int(byte, 2))

And of course, it would be faster to use bit manipulation. Instead of appending to a string, you can use << (left shift) and | (bitwise or).

EDIT: For the actual writing, you can use:

handleW.write(bytes([char]))

This creates and writes a bytes from a list consisting of a single number.

EDIT 2: Correction, it should be:

handleW.write(bytes([int(byte, 2)]))

There is no need to use chr.

answered Jun 25, 2010 at 5:21
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer, but how can I directly (with wb mode) write (str)"11011011" into file? No I get "TypeError: can't write str to binary stream"
Thank you so much. Final working and version is handleW.write(bytes(chr(int(byte, 2)), "utf-8"))
@Mikk, my previous answer was partly wrong. It can just be handleW.write(bytes([int(byte, 2)])).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.