Created
July 18, 2022 16:21
-
-
Save jackbonaguro/233c53af3ef9c92ce3243719c5217f63 to your computer and use it in GitHub Desktop.
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/env python | |
| # coding: utf-8 | |
| """ | |
| String to Brainfuck. | |
| Converts a string to a brainfuck code that prints that string. | |
| Author: j0hn <[email protected]> | |
| """ | |
| """ | |
| 7/17/2022 | |
| Modified to accept a binary hex string (without `0x` prefix) as input rather than a string | |
| Author: jackb4040 <[email protected]> | |
| """ | |
| import sys | |
| import math | |
| def val2bf(val): | |
| """Convert a char to brainfuck code that prints that char.""" | |
| result_code = "" | |
| ascii_value = val | |
| factor = math.floor(ascii_value / 10) | |
| remaining = ascii_value % 10 | |
| result_code += "%s\n" % ("+" * 10) | |
| result_code += "[\n" | |
| result_code += " >\n" | |
| result_code += " %s\n" % ("+" * factor) | |
| result_code += " <\n" | |
| result_code += " -\n" | |
| result_code += "]\n" | |
| result_code += ">\n" | |
| result_code += "%s\n" % ("+" * remaining) | |
| result_code += ".\n" | |
| result_code += "[-]\n" | |
| return result_code | |
| def str2bf(string): | |
| """Convert a string to brainfuck code that prints that string.""" | |
| result = "" | |
| # for char in string: | |
| for i in range(math.floor(len(string) / 2)): | |
| val = int(string[i*2:(i*2)+2], 16) | |
| # print("val:", val) | |
| result += val2bf(val) | |
| return result | |
| def print_help(): | |
| """Print the help message.""" | |
| message = "python %s: missing arguments\n\n" % sys.argv[0] | |
| message += "Usage: %s [OPTIONS] STRING\n" % sys.argv[0] | |
| message += "Options:\n" | |
| message += " -h, --help displays this help message.\n" | |
| message += " -s, --small prints the code in one liner.\n" | |
| message += " -n, --newline adds a new line character " | |
| message += "at the end of the string.\n" | |
| sys.stderr.write(message) | |
| def main(): | |
| """Reads the arguments from stdin and outputs the code.""" | |
| if len(sys.argv) < 2: | |
| print_help() | |
| sys.exit(0) | |
| add_new_line = False | |
| small_output = False | |
| if "-n" in sys.argv or "--newline" in sys.argv: | |
| add_new_line = True | |
| try: | |
| sys.argv.remove("-n") | |
| except ValueError: | |
| sys.argv.remove("--newline") | |
| if "-s" in sys.argv or "--small" in sys.argv: | |
| small_output = True | |
| try: | |
| sys.argv.remove("-s") | |
| except ValueError: | |
| sys.argv.remove("--small") | |
| if "-h" in sys.argv or "--help" in sys.argv: | |
| print_help() | |
| sys.exit(0) | |
| input_string = " ".join(sys.argv[1:]) | |
| result = str2bf(input_string + ("\n" * add_new_line)) | |
| if small_output: | |
| result = result.replace(" ", "").replace("\n", "") | |
| print(result) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment