Last active
March 15, 2017 04:09
-
-
Save frank184/3bc4b56bb92daf80d2e229e93dc988ee to your computer and use it in GitHub Desktop.
Simple ASCII converter CLI in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import sys | |
| def main(): | |
| if len(sys.argv[1:]) < 2: | |
| usage() | |
| elif sys.argv[1] == '-d' or sys.argv[1] == '--decode': | |
| decode(sys.argv[2:]) | |
| elif sys.argv[1] == '-e' or sys.argv[1] == '--encode': | |
| encode(" ".join(sys.argv[2:])) | |
| else: | |
| usage() | |
| sys.exit(0) | |
| def decode(nums): | |
| try: | |
| print "".join([chr(int(n)) for n in nums]) | |
| except: | |
| usage() | |
| def encode(string): | |
| try: | |
| print " ".join([str(ord(c)) for c in string]) | |
| except: | |
| usage() | |
| def usage(): | |
| print "Usage:" | |
| print "./ascii [operation] [data]" | |
| print "Operations:" | |
| print "-e --encode Encodes ASCII characters to their int representation" | |
| print "-d --decode Decodes ASCII characters from their int representation" | |
| print "Examples:" | |
| print "./ascii -e ABCDEFG" | |
| print "./ascii -d 65 66 67 68 69 70 71" | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment