0

make a instance of numbers and string. atoi() converts the string argument str to an integer.So how to converts bytes argument to string in python? such as

import sys
string = sys.argv[1]
print(string)
python2 test.py "\000\000\000"

with the result of "\000\000\000" which makes no sense, but I want to converts it to '\x00\x00\x00'

joey.li
  • 1
  • 1
  • https://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3 does this help? – jsofri Oct 21 '21 at 08:08
  • Can you please clarify what you are asking? ``'\x00\x00\x00'`` and ``"\000\000\000"`` are the exact same string. ``sh test.py "\000\000\000"`` runs the Python code *as a shell script* which is just bogus. – MisterMiyagi Oct 21 '21 at 08:35
  • @MisterMiyagi I am sorry that I have some wrong with my question above, and I have amended it.And I have got what I want, thanks. – joey.li Oct 21 '21 at 12:53

1 Answers1

1

IIUC, you want to decode the representation of a string. But you are not passing a correct representation because the quotes have been eaten by the shell. If you do, you will be able to use literal_eval to convert a representation. Demo (I have added the length of the string to make sure of what it really is:

import sys
import ast
string = ast.literal_eval(sys.argv[1])
print(len(string), repr(string))

You can then type:

python test.py '"\000\000\000"'

to get:

3 '\x00\x00\x00'
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252