0

In Python 3, I have a string like the following:

mystr = "\x00\x00\x01\x01\x80\x02\xc0\x02\x00"

This string was read from a file and it is the bytes representation of some text. To be clear, this is a unicode string, not a bytes object.

I need to transform mystr into a bytes object like the following:

mybytes = b"\x00\x00\x01\x01\x80\x02\xc0\x02\x00"

Notice that the translation should be literal. I don't want to encode the string.

Running .encode('utf-8') will escape the \.

It I manually copy and past the content into a bytes string, then everything works. What I couldn't find anywhere is how could I convert it without copy+paste.

Serenity
  • 32,301
  • 18
  • 107
  • 109
Leo Uieda
  • 203
  • 3
  • 6
  • `bytes(bytearray(ord(i) for i in mystr))` seems to work ... though I feel like there should be a better way. Maybe the better way is to figure out how to not end up in this situation in the first place? :-) – mgilson Jul 13 '16 at 00:39
  • @mgilson thanks! I was thinking about that but this is what I have. Reading the file in `'rb'` gives be `"\\x00\\x00..."`, which is not what I want. Looking for something unrelated I found the solution I posted below. – Leo Uieda Jul 13 '16 at 00:42
  • I ended up deleting my answer because it didn't really work. There were some extra characters being printed in the middle that I hadn't noticed before. – Leo Uieda Jul 13 '16 at 00:53

1 Answers1

2

mystr.encode("latin-1") is what you want.

Paul Cornelius
  • 7,234
  • 1
  • 11
  • 17