I forgot somewhere in the original story, but I ported some code such as Ruby that had fallen somewhere.
# -*- coding: utf-8 -*-
"""
convert_table is a list of characters.
Use this list for radix conversion.
If the length of the list is 62, it feels like converting to 62 base.
Enter int in decimal.
"""
import math
def string_to_int(target, convert_table):
def char_to_int(char):
return convert_table.index(char)
int_result = 0
target = [char for char in target]
target.reverse()
for index, char in enumerate(target):
int_result += char_to_int(char) * int(math.pow(len(convert_table), index))
return int_result
def int_to_string(target, convert_table):
def int_to_char(int_value):
return convert_table[int_value]
string_result = ""
while target > 0:
string_result = int_to_char(target % len(convert_table)) + string_result
target /= len(convert_table)
return string_result
def generate_convert_table():
convert_table_base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
convert_table_base_list = list(convert_table_base)
import random
random.shuffle(convert_table_base_list)
return "".join(convert_table_base_list)
Recommended Posts