1

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.

gb_away
  • 51
  • 4
  • Try adding this for more clarity about what sys.argv[1] is.`print(repr(sys.argv[1]))` – Ken Kinder Jun 11 '20 at 16:23
  • @KenKinder it returns: '@\x11\udcb8', so I'm assuming it's unicode. Doesn't clarify much tho, as I've already knew it was twice as big it should be. – gb_away Jun 11 '20 at 16:33
  • Well, it's definitely a binary string with something. Perhaps [this may shed some light on it for you](https://stackoverflow.com/questions/5408730/what-is-the-encoding-of-argv) – Ken Kinder Jun 11 '20 at 16:52

0 Answers0