0

I'm trying to mask a string by encoding it like this:

>>> 'monkey'.encode('utf-16') b'\xff\xfem\x00o\x00n\x00k\x00e\x00y\x00' 

I then copy \xff\xfem\x00o\x00n\x00k\x00e\x00y\x00 and paste it into a file file.txt.

Then I want to read that file in and decode it.

>>> with open('file.txt', 'rb') as f: >>> bytes_object = next(f).strip() # assume file has only one line >>> bytes_object b'\\xff\\xfem\\x00o\\x00n\\x00k\\x00e\\x00y\\x00' 

The \'s are being escaped... not what I want.

Decoding does not work as expected.

>>> bytes_object.decode('utf-16') '硜晦硜敦屭へ漰硜〰屮へ欰硜〰履へ礰硜〰' 

Clearly I am not understanding something here. Is there any way to read and decode a file that has the text \xff\xfem\x00o\x00n\x00k\x00e\x00y\x00?

3
  • Because if you copy and paste it copies the representation not the actual bytes. Commented May 8, 2017 at 23:51
  • Instead of copy-pasting into a text file, write the bytes directly to a file using Python. Commented May 8, 2017 at 23:54
  • Thanks @juanpa.arrivillaga, what I was looking for was eval(bytes_object).decode('utf-16') Commented May 9, 2017 at 4:18

2 Answers 2

2

Copying and pasting the representation of the bytes object isn't a useful thing to do. If you want to create a UTF-16 file, try this:

with open('file.txt', 'wb') as fp: message = 'monkey'.encode('UTF16') fp.write(message) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Rob, this is absolutely the right way to write the bytes to a file (as opposed to what I am doing above)
1

The other answers are accurate, in that writing the bytes themselves to a file is more straightforward than copying and pasting the repr() form (with the backslash-x's).

But your question was about how to parse that form, and it is possible using eval(), which is basically the opposite of repr(). Here's an interactive version that doesn't use the file write/read but performs the operations in question.

>>> s = 'monkey' >>> e = s.encode('utf-16') >>> x = str(e) >>> x "b'\\xff\\xfem\\x00o\\x00n\\x00k\\x00e\\x00y\\x00'" >>> eval(x) b'\xff\xfem\x00o\x00n\x00k\x00e\x00y\x00' >>> eval(x).decode('utf-16') 'monkey' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.