Convert given number to a list for every number do the following formula x * 2 + number repeat for each result! Example: binary number = 100110 0 x 2 + 1 = 1 1 x 2 + 0 = 2 2 x 2 + 0 = 4 4 x 2 + 1 = 9 9 x 2 + 1 = 19 19 x 2 + 0 = 38 :param n: binar
(n, visualize=False)
| 45 | |
| 46 | |
| 47 | def to_base10(n, visualize=False): |
| 48 | """ |
| 49 | Convert given number to a list |
| 50 | for every number do the following formula |
| 51 | |
| 52 | x * 2 + number |
| 53 | |
| 54 | repeat for each result! Example: |
| 55 | |
| 56 | binary number = 100110 |
| 57 | |
| 58 | 0 x 2 + 1 = 1 |
| 59 | 1 x 2 + 0 = 2 |
| 60 | 2 x 2 + 0 = 4 |
| 61 | 4 x 2 + 1 = 9 |
| 62 | 9 x 2 + 1 = 19 |
| 63 | 19 x 2 + 0 = 38 |
| 64 | |
| 65 | :param n: binary number |
| 66 | :param visualize: Show process |
| 67 | :return: decimal number |
| 68 | """ |
| 69 | if type(n) is str: |
| 70 | raise ValueError('value must be int not type {}'.format(str(type(n)))) |
| 71 | |
| 72 | x = 0 |
| 73 | _list = [int(i) for i in str(n)] |
| 74 | |
| 75 | for number in _list: |
| 76 | if visualize: |
| 77 | print("{} x 2 + {} = {}".format(str(x), str(number), str(x * 2 + number))) |
| 78 | x = x * 2 + number |
| 79 | |
| 80 | return x |
| 81 | |
| 82 | |
| 83 | def to_base16(n, visualize=False): |