Created
February 15, 2024 01:15
-
-
Save MurageKibicho/75baa9ce73bd3d1b7ba66edfd4f02e05 to your computer and use it in GitHub Desktop.
Python base converter
This file contains 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
def base2_to_base10(binary_string): | |
return int(binary_string, 2) | |
def base10_to_base256(decimal_number): | |
return decimal_number.to_bytes((decimal_number.bit_length() + 7) // 8, 'big') | |
def base256_to_base10(byte_array): | |
return int.from_bytes(byte_array, 'big') | |
def base10_to_base2(decimal_number): | |
return bin(decimal_number)[2:] | |
def bytes_to_base256(byte_array): | |
return '[' + ', '.join(str(byte) for byte in byte_array) + ']' | |
# Example usage: | |
binary_number = "010011010100110100101010101000010101100101011011011011010110110110110101101101100011101000110100011101000110010001010111010101110101011101010111010101111101011101010011010101111101011110110111" # Base 2 number | |
decimal_number = base2_to_base10(binary_number) | |
print("Decimal:", decimal_number) | |
bytes_array = base10_to_base256(decimal_number) | |
print("Bytes:", bytes_to_base256(bytes_array)) | |
decimal_number_again = base256_to_base10(bytes_array) | |
print("Decimal again:", decimal_number_again) | |
binary_number_again = base10_to_base2(decimal_number_again) | |
print("Binary again:", binary_number_again) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment