0

I want to concatenate a hexadecimal digit hh with \x, to make a string "\xhh" that I will then put into a list and convert that into a long string. Here is my current code for that, which raises an error:

ValueError: invalid \x escape

This error makes sense to me, since I believe that \x has to be attached to something in order for the computer to recognize it, and I'm not sure how I can do that. I am wondering, would something like "\" + "x" + hh work, and also, is there a more efficient way of doing this?

Here is my code as it stands when it caused the error: (Note - volt, curr, and checksum values vary. I just put example values in for this)

voltValues = [30, 30, 30]
currValues = [30, 30, 30]
checksumValues = [39, 33]
v1 = "\x" + voltValues[0]
v2 = "\x" + voltValues[1]
v3 = "\x" + voltValues[2]
c1 = "\x" + currValues[0]
c2 = "\x" + currValues[1]
c3 = "\x" + currValues[2]
cs1 = "\x" + checksumValues[0]
cs2 = "\x" + checksumValues[1]

list = [ "\x01", "\x30", "\x53", v1, v2, v3, c1, c2, c3, "\x46", "\x46", "\x46", "\x30", "\x30", "\x30", "\x32", "\x46", "\x46", cs1, cs2, "\x0D" ]
return ''.join(list)

I want it to return "\x01\x30\x53\x30\x30\x30\x30\x30\x30\x46\x46\x46\x30\x30\x30\x32\x46\x46\x39\x33\x0D".

2
  • 1
    You should know, 30 and \x30 means different number, you really want to mix them? Commented Dec 3, 2013 at 19:34
  • What are you actually trying to accomplish here? Are you aware of the struct module? Commented Dec 3, 2013 at 19:36

3 Answers 3

3

You can shortcut the whole process by saying int(s, 16) to convert each two-character hex code s into an integer, and chr() to convert it further into a character. For example, chr(int('4A', 16)) returns 'J'. Your attempted concatenation of '\x' fails because hex codes like \xAA are interpreted as literals in the Python code itself, at the time when the interpreter reads it.

But by the way, the volt values in your program are not expressed in hex to start with. Or as strings. The following all give the same result:

v = 0x4A   # the proper way to write int literals in hex
print chr(v)

v = 74     # without the 0x, this is interpreted as decimal
print chr(v)

v = '4A'   # in quotes, it can be anything you like, but then you need int(v,16)
print chr(int(v, 16))
Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly. Thank you! I do in fact have the voltValues as a list of strings in my program - I forgot to put the " 's in the example code that I posted here.
1

The way you have it currently, Python is creating a string with value "\x" and then tries to add 30 to it (which will fail, because that would be concatenating a str and an int).

What you want to do would be something like this:

v1 = chr(int(str(voltValues[0]), 16))

This will first make a string from your value, so that int() can parse it. It then parses this string at the base 16 (it parses it as hex data). After that, it creates the string that has an encoding of the integer you just created. This means that e. g.

>>> chr(0x30)
'0'
>>> "\x30"
'0'
>>> chr(0x41)
'A'
>>> "\x41"
'A'

Comments

0

You want to look at the chr built-in function.

v1 = chr(voltValues[0])

will set v1 to '\x1e'.

If you wanted that to be hex, then setting voltValues[0] to 0x30 will yield a v1 of '0'.

Comments

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.