4

I need to add an escape character to a variable which I'm appending to another string and have it apply its effects. This is what I have:

h1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
h2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']

h3 = list(itertools.product(h1, h2))
h4 = []

for item in h3:
    h4.append(''.join(item))

temp = r'\x' + str(h4[0]) + '\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c'

So if i have \xh I need the character with hex value hh but I can't seem to find anything in python that does this besides \x which I can't seem to use on variables.

Any suggestions?

3
  • Why are you computing all of the products, but only using one value? Commented Sep 22, 2016 at 22:27
  • Clarification: You don't really want "escapes". Escapes are for string literals to represent raw byte values (from 0 to 255) or raw Unicode ordinals (for Py2 unicode or Py3 str), you don't need to actually make an escape code to convert ordinals to bytes/str values. The answers are explaining how you skip the escapes and get what you actually want? Commented Sep 22, 2016 at 22:38
  • XY problem Commented Sep 23, 2016 at 18:31

3 Answers 3

3

A somewhat faster solution that repeated int/chr calls (assuming you're using more than just the first byte produced) is to create a complete hex string and parse it all at once:

import itertools
import binascii

hexdigits = "123456789abcdef"
completehex = ''.join(map(''.join, itertools.product(hexdigits, repeat=2)))
completebytes = binascii.unhexlify(completehex)

This will bulk decode all the hexpairs into the raw byte values (the "escapes" you want), so completebytes would be '\x00\x01\x02\x03...\xfd\xfe\xff'.

Of course, for this specific case (if your real problem isn't just generating all possible bytes values in order), you could simplify it even further, because what you're doing is just generating all possible byte values:

# Py3
completebytes = bytes(range(256))

# On Py2, bytes is alias for str, so must use bytearray first to accept iterable of int
completebytes = bytes(bytearray(range(256)))

Or, just for fun, the fastest possible way abusing maketrans:

# Py3:
completebytes = bytes.maketrans(b'', b'')  # Add .decode('latin-1') if you really want str

# Py2:
import string
completebytes = string.maketrans('', '')
Sign up to request clarification or add additional context in comments.

Comments

1

Use int() to convert your hex value to an integer and then chr() to convert that number to a character:

import itertools

hexdigits = "123456789abcdef"

for dig1, dig2 in itertools.product(hexdigits, hexdigits):
    char = chr(int(dig1 + dig2, 16))
    temp = char + '\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c'

Comments

0

To answer the OP question, here is a way to convert, using the escape notation, a string containing two hexadecimal digits to a character

h = '11'
temp = eval( r"'\x" + h + "'" )

It is not, however, the best way to do the conversion (see other answers). I would suggest chr(int(h,16)). Another way would be to use integers instead of strings h=0x11 and temp = chr(h).

2 Comments

If you actually want to make escaped literals and have Python process them, ast.literal_eval is better than eval (it avoids eval's risk of arbitrary code execution), and decodeing with the string_escape/unicode_escape codecs (in Py3, only the latter exists) is better still. Just thought I'd mention these better options to look at before eval; eval should be at the absolute bottom of any hierarchy of options.
You are right. That's what I meant when I wrote "It is not the best way [...]" (I probably did not insist enough). I would also recommend using eval as a last option. However, the OP question was how to add escape characters in a variable.

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.