I'm currently trying to print an element of a bytestring in hexadecimal format, after shifting it, like this:
Python 3.6.10 (default, May 12 2020, 10:44:31)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = '\x40\x11\xb8'
>>> type(x)
<class 'str'>
>>> print(r'\x{:x}'.format(ord(x[2])-11))
\xad
>>> quit()
In the interpreter this works just fine, however when I try to pass the string as an argument, it interprets the last value as something longer than a bytestring, possibly utf-8:
cat test.py
#!/usr/bin/env python
import sys
print(type(sys.argv[1]))
print(r'\x{:x}'.format(ord(sys.argv[1][2])-11))
./test.py $'\x40\x11\xb8'
<class 'str'>
\xdcad
So the question would be: is there a way to prevent that from happening?
I've also tried mapping sys.argv[1] to a bytearray, but since it exceeds the 255 limit in sys.argv[1][2] it doesn't work.