How to avoid the "ValueError: invalid \x escape" error in this special case?
15,171
Solution 1
chr()
will give you the bytestring for a value between 0 and 255.
>>> chr(0xd3)
'\xd3'
Solution 2
Ignacio answer is the correct one, but there is also the Dark side of the Force:
>>> your_string = r'\x48\x65\x6c\x6c\x6f\x2c\x20\x57\x6f\x72\x6c\x64\x21'
>>> your_string.decode('string-escape')
'Hello, World!'
So you could have fixed your problem using a raw literal(r'\x' instead of '\x'), and converting the escapes into characters with the str.decode
method.
Author by
JohnnyFromBF
Updated on July 11, 2022Comments
-
JohnnyFromBF almost 2 years
Obviously the wildcard %x is not recognized as a byte hex value, so I get the error "ValueError: invalid \x escape".
How to avoid this? I'm not familiar with python.
for i in xrange(0,length): if i % 2 == 0: tempArray.append(unpack("B",payload_raw[x])[0]^key) x += 1 else: randomByte = random.randint(65,90) tempArray.append(randomByte) for i in range(0,len(tempArray)): tempArray[i]="\x%x"%tempArray[i] for i in range(0,len(tempArray),15): outArray.append("\n'"+"".join(tempArray[i:i+15])+"'") outArray = "".join(outArray) devide = "i % 2;" open_structure = open(structure).read() code = open_structure % (junkA,outArray,junkB,key,length,devide) b.write(code) b.flush()