Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python convert base

# 10 numbers + 26 upper case letters + 26 lower case letters + 128 printable ASCII characters = 190
alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜø£Ø׃áíóúñѪº¿®¬½¼¡«»░▒▓│┤ÁÂÀ©╣║╗╝¢¥┐└┴┬├─┼ãÃ╚╔╩╦╠═╬¤ðÐÊËÈıÍÎÏ┘┌█▄¦Ì▀ÓßÔÒõÕµþÞÚÛÙýݯ´≡±‗¾¶§÷¸°¨·¹³²■'
print('alphabet =', len(alphabet))

Number = '12020202002020202202020200101010110101010040404044040404004040404'


def base_encoder(number, base):
    if base > len(alphabet):
        print('base not supported')

    is_negative = number < 0
    number = abs(number)
    base_number = ''

    while number:
        number, i = divmod(number, base)
        base_number = alphabet[i] + base_number
    if is_negative:
        base_number = '-' + base_number

    return base_number


def base_decoder(number, base):
    if base > len(alphabet):
        print('base not supported')
    dict_of_alph = {d: i for i, d in enumerate(alphabet)}

    base_number = sum(dict_of_alph[h] * base ** pos for pos, h in enumerate(reversed(number)))

    return base_number


def base_changer(number, in_bace, out_bace):
    print(number, 'this is bace', in_bace)
    step_0 = base_decoder(number, in_bace)
    print(step_0, 'this is base 10')
    step_1 = base_encoder(step_0, out_bace)
    print(step_1, 'this is bace', out_bace)
    return step_1


step_2 = base_changer(Number, 5, 150)
step_3 = base_changer(step_2, 150, 5)
Comment

PREVIOUS NEXT
Code Example
Python :: python argparse 
Python :: how to add column headers in pandas 
Python :: numpy.datetime64 to datetime 
Python :: python console command 
Python :: python scond max function 
Python :: Make solutions faster in python 
Python :: do you have to qualift for mosp twice? 
Python :: check version numpy 
Python :: python turtle window not responding 
Python :: delete a record by id in flask sqlalchemy 
Python :: python logging to console exqmple 
Python :: for loop with float python 
Python :: # find the common elements in the list. 
Python :: python product of list 
Python :: producer consumer problem using queue python 
Python :: how to send a message from google form to a python 
Python :: append one column pandas dataframe 
Python :: opencv imshow resize 
Python :: cv2.adaptiveThreshold() 
Python :: saving to csv without the index 
Python :: how to change number of steps in tensorflow object detection api 
Python :: combinations python 
Python :: python namedtuple 
Python :: python change base function 
Python :: pandas subtract integer from column 
Python :: convert 2d list to 1d python 
Python :: arithmetic python string 
Python :: count number of words in a string python 
Python :: import python module from another directory 
Python :: python split list of tuples in two lists 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =