-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex_encode.py
More file actions
executable file
·38 lines (28 loc) · 1017 Bytes
/
hex_encode.py
File metadata and controls
executable file
·38 lines (28 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
"""Hex encoder: emit a string as a sequence of \\xHH bytes.
Usage:
python3 hex_encode.py # interactive (type 'exit' to quit)
python3 hex_encode.py "hello world" # one-shot, encode the argument
Authorized testing only.
"""
import argparse
import sys
def encode(s: str) -> str:
return "".join("\\x" + format(ord(c), "02x") for c in s)
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("text", nargs="?", help="String to encode. If omitted, run interactively.")
args = parser.parse_args()
if args.text is not None:
print(encode(args.text))
return 0
try:
while True:
s = input("Enter text: ")
if s.lower() == "exit":
return 0
print("HEX\t==>\t" + encode(s))
except (EOFError, KeyboardInterrupt):
return 0
if __name__ == "__main__":
sys.exit(main())