Created
April 8, 2020 18:45
-
-
Save Luke-zhang-04/7db5084fc9793f9ead01d5c023225716 to your computer and use it in GitHub Desktop.
Binary to Hexadecimal code (also a test for gists)
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
| #bruh what is a gist | |
| hexes = {"A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15} | |
| hexes_reversed = {10: "A", 11: "B", 12: "C", 13: "D", 14: "E", 15: "F"} | |
| class Decimal: #everything from decimal/base 10 | |
| @staticmethod | |
| def to_binary(number): #decimal to binary | |
| value = int(number) | |
| total = [] | |
| while True: | |
| total.append(value % 2) #get remaidner | |
| value /= 2 #divide | |
| #strip decimals (integer division // rounds the number instead of stripping decimals off completely) | |
| value = int(value) | |
| if value == 0: break | |
| total = total[::-1] #reverse the list | |
| return ''.join(map(str, total)) #total is an array, return a string | |
| @staticmethod | |
| def to_hex(number): #decimal to hex | |
| value = int(number) | |
| total = [] | |
| while True: | |
| remainder = value % 16 #get remainder | |
| if remainder in hexes_reversed: remainder = hexes_reversed[remainder] #if remainder <= 10, replace with a letter | |
| total.append(remainder) #append remainder | |
| value /= 16 #divide | |
| value = int(value) #strip decimals | |
| if value == 0: break | |
| total = total[::-1] | |
| return ''.join(map(str, total)) #total is an array, return a string | |
| @staticmethod | |
| def to_base(number, givenBase): #decimal to base anything | |
| value = int(number) | |
| givenBase = int(givenBase) | |
| total = [] | |
| while True: | |
| total.append(value % givenBase) #get remaidner | |
| value /= givenBase #divide | |
| value = int(value) #strip decimals (integer division // rounds the number instead of stripping decimals off completely) | |
| if value == 0: break | |
| total = total[::-1] #reverse the list | |
| return ''.join(map(str, total)) #total is an array, return a string | |
| class Binary: #everything from binary | |
| @staticmethod | |
| def to_decimal(number): #binary to decimal/base 10 | |
| #In general, for number abcd, with n base a*n**index(a) + b*n**index(b) + c*n**index(c) + d*n**index(d) | |
| total = 0 | |
| for i in range(1, len(number)+1): total += int(number[-i])*2**(i-1) | |
| return total | |
| @staticmethod | |
| def to_hex(number): #binary to hex | |
| total = Binary.to_decimal(number) #first binary to decimal | |
| total = Decimal.to_hex(total) #then the decimal number to binary | |
| return total | |
| @staticmethod | |
| def to_base(number, givenBase): | |
| total = Binary.to_decimal(number) | |
| total = Base.to_binary(number, givenBase) | |
| return total | |
| class Hexadecimal: #everything from hexadecimal | |
| @staticmethod | |
| def to_decimal(number): #hex to decimal/base 10 | |
| #In general, for number abcd, with n base a*n**index(a) + b*n**index(b) + c*n**index(c) + d*n**index(d) | |
| total = 0 | |
| values = list(number) | |
| for i in range(len(values)): | |
| if values[i].upper() in hexes: values[i] = hexes[values[i].upper()] #If a letter exists, replace it with it's corresponding base 10 value | |
| for i in range(1, len(values)+1): total += int(values[-i])*16**(i-1) | |
| return total | |
| @staticmethod | |
| def to_binary(number): #hex to bin | |
| total = Hexadecimal.to_decimal(number) #first bin to dec | |
| total = Decimal.to_binary(total) #then dex to bin | |
| return total | |
| @staticmethod | |
| def to_base(number, givenBase): | |
| total = Hexadecimal.to_decimal(number) | |
| total = Decimal.to_base(number, givenBase) | |
| return total | |
| class Base: #everything from a given base | |
| @staticmethod | |
| def to_decimal(number, givenBase): #any base to decimal value | |
| #In general, for number abcd, with n base a*n**index(a) + b*n**index(b) + c*n**index(c) + d*n**index(d) | |
| total = 0 | |
| for i in range(1, len(number)+1): total += int(number[-i])*int(givenBase)**(i-1) | |
| return total | |
| @staticmethod | |
| def to_binary(number, givenBase): | |
| total = Base.to_decimal(number, givenBase) | |
| total = Decimal.to_binary(total) | |
| return total | |
| @staticmethod | |
| def to_hex(number, givenBase): | |
| total = Base.to_decimal(number, givenBase) | |
| total = Decimal.to_hex(total) | |
| return total | |
| @staticmethod | |
| def to_base(number, givenBase, newBase): | |
| total = Base.to_decimal(number, givenBase) | |
| total = Decimal.to_base(total, newBase) | |
| return total | |
| def _test(): | |
| choice = input().split(" ") | |
| if choice[1] in ["dec", "decimal"]: #from dec | |
| if choice[3] in ["bin", "binary"]: | |
| print("binary:", Decimal.to_binary(choice[-1])) #to bin | |
| elif choice[3] in ["hex", "hexadecimal"]: | |
| print("hexadecimal:", Decimal.to_hex(choice[-1])) #to hex | |
| elif choice[3] in ["base", "any"]: | |
| print("base", choice[4] + ":", Decimal.to_base(choice[-1], choice[4])) #to a given base | |
| elif choice[1] in ["bin", "binary"]: #from bin | |
| if choice[3] in ["dec", "decimal"]: | |
| print("decimal/base 10:", Binary.to_decimal(choice[-1])) #to dec | |
| elif choice[3] in ["hex", "hexadecimal"]: | |
| print("hexadecimal:", Binary.to_hex(choice[-1])) #to hex | |
| elif choice[3] in ["base", "any"]: | |
| print("base", choice[4] + ":", Binary.to_base(choice[-1], choice[4])) #to a given base | |
| elif choice[1] in ["hex", "hexadecimal"]: #from hex | |
| if choice[3] in ["dec", "decimal"]: | |
| print("decimal/base 10:", Hexadecimal.to_decimal(choice[-1])) #to dec | |
| elif choice[3] in ["bin", "binary"]: | |
| print("binary:", Hexadecimal.to_binary(choice[-1])) #to bin | |
| elif choice[3] in ["base", "any"]: | |
| print("base", choice[4] + ":", Hexadecimal.to_base(choice[-1], choice[4])) | |
| elif choice[1] in ["base", "any"]: #from any base | |
| if choice[4] in ["dec", "decimal"]: | |
| print("decimal/base 10:", Base.to_decimal(choice[-1], choice[2])) #to dec | |
| elif choice[4] in ["bin", "binary"]: | |
| print("binary:", Base.to_binary(choice[-1], choice[2])) #to bin | |
| elif choice[4] in ["hex", "hexadecimal"]: | |
| print("hexadecimal:", Base.to_hex(choice[-1], choice[2])) #to hex | |
| elif choice[4] in ["base", "any"]: | |
| print("base", choice[5] + ":", Base.to_base(choice[-1], choice[2], choice[5])) #to a given base | |
| print() #add new line | |
| _test() | |
| if __name__ == "__main__": | |
| print("inputs in the form of from <current type> to <desired type> <number>\ne.g from dec to bin -3, from binary to hex 1011, from bin to hexadecimal 34FA5", "from dec to base 3 2353") | |
| _test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment