DMSilva
DMSilva

Reputation: 185

Convert string to bytes literally

I want to take a string such as:

'\\xeb\\x4d' 

and turn it into:

b'\xeb\x4d'

If I do:

bytes('\\xeb\\x4d', 'utf-8')

I get:

b'\\xeb\\x4d'

I need something that does the following:

something('\\xeb\\x4d') == b'\xeb\x4d'

Upvotes: 2

Views: 1627

Answers (2)

Mark Tolonen
Mark Tolonen

Reputation: 177406

>>> a = '\\xeb\\x4d'   # a Unicode string
>>> a.encode('latin1') # get a byte string
b'\\xeb\\x4d'
>>> a.encode('latin1').decode('unicode_escape') # unescape, get a Unicode string
'ëM'
>>> a.encode('latin1').decode('unicode_escape').encode('latin1') # get a byte string
b'\xebM'
>>> a.encode('latin1').decode('unicode_escape').encode('latin1') == b'\xeb\x4d'
True

Note that latin1 is the first 256 codepoints of Unicode, so encoding the first 256 bytes of Unicode gives the same byte values as the original codepoint.

Upvotes: 1

DMSilva
DMSilva

Reputation: 185

a = '\\xeb\\x4d'
a = bytes(a, 'utf-8')
a = a.decode('unicode_escape').encode('latin1')

gives

b'\xebM'

because

'\x4d' == 'M'

Upvotes: 1

Related Questions