commit 24fd1c460c33b506373d388378d665eb850f5132 Author: Abheek Dhawan Date: Tue Feb 8 20:55:55 2022 -0600 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/converter.py b/converter.py new file mode 100644 index 0000000..c0478c3 --- /dev/null +++ b/converter.py @@ -0,0 +1,37 @@ +def decToBin(decIn): + ans = int(decIn) + ret = "" + twoMult = int(1) + currentTwoPwr = int(0) + while ans != 0: + ''' + sleep(0.1) + print("TwoMult = " + str(twoMult)) + print("ans = " + str(ans)) + ''' + if (twoMult < ans / 2): # If the current power of 2 is less than half that of the decimal, then double it and increment the power + twoMult *= 2 + currentTwoPwr += 1 + continue + else: + if twoMult <= ans: + ans -= twoMult + ret += "1" + else: + ret += "0" + currentTwoPwr -= 1 + twoMult = 2 ** currentTwoPwr + + return int(ret) + +def binToDec(binIn): + ans = str(binIn) + ret = 0 + currentTwoPwr = 0 + for i in reversed(ans): + if (int(i) == 1): + # print("Ret += 2 ** " + str(i)) + ret += 2 ** currentTwoPwr + currentTwoPwr += 1 + + return ret \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..798ddf6 --- /dev/null +++ b/main.py @@ -0,0 +1,55 @@ +from converter import decToBin, binToDec + +# True: convert from decimal to binary +# False: convert from binary to decimal +convertToBinary = True + +print(''' ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ _________ ____ ____ ____ ____ ____ ____ ____ ____ ____ +||B |||i |||n |||a |||r |||y |||/ |||D |||e |||c |||i |||m |||a |||l ||| |||C |||o |||n |||v |||e |||r |||t |||e |||r || +||__|||__|||__|||__|||__|||__|||__|||__|||__|||__|||__|||__|||__|||__|||_______|||__|||__|||__|||__|||__|||__|||__|||__|||__|| +|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/_______\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\| +''') + +print("""Would you like to convert from +1. Decimal to binary +2. Binary to decimal +""") + +# Get choice +choiceIn = int(input()) + +# Get 1 or 2, if neither then keep looping +while True: + match choiceIn: + case 1: + convertToBinary = True + break + case 2: + convertToBinary = False + break + case _: + print("Not a valid choice.") + continue + +if (convertToBinary): + print(''' + ====================================================================== + CONVERTING DECIMAL TO BINARY (please separate your inputs with spaces) + ====================================================================== + ''') + decListIn = map(int, input().split(" ")) + print ("\n\nOutput:") + for i in decListIn: + print(decToBin(i)) +else: + print(''' + ====================================================================== + CONVERTING BINARY TO DECIMAL (please separate your inputs with spaces) + ====================================================================== + ''') + print ("\nOutput:\n") + binListIn = map(str, input().split(" ")) + for i in binListIn: + print(binToDec(i)) + +print("\n\n") \ No newline at end of file