numeration.py

Created by zetamap

Created on November 11, 2022

2.92 KB

Convert a text to Hexa, Binary or Decimal, and vice versa.

Posibles conversion: 0- Text → Hexa
1- Text → Decimal
2- Text → Octal
3- Text → Binary
4- Hexa → Text
5- Hexa → Decimal
6- Hexa → Octal
7- Hexa → Binary
8- Decimal → Text
9- Decimal → Hexa
10-Decimal → Octal
11-Decimal → Binary
12-Octal → Text
13-Octal → Hexa
14-Octal → Decimal
15-Octal → Binary
16-Binary → Text
17-Binary → Hexa
18-Binary → Decimal
19-Binary → Octal

Display of list may come out of the screen a little, because the script was designed with the small python font, but that does not interfere with its operation


def HEX(i, l):
  h = hex(i)[2:]
  c = len(h)/l
  return '0'*((int(c)+(0 if int(c) == c else 1))*l-len(h))+h
TEXT2HEX = lambda s, l: ''.join([HEX(ord(i), l) for i in s])
HEX2TEXT = lambda s, l: ''.join([chr(int("0x"+s[i:i+l])) for i in range(0, len(s), l)])
__CONVERT_MODES = [
  {"label": "Text -> Hexa", "method": TEXT2HEX},
  {"label": "Text -> Decimal", "method": lambda v, l: str(int("0x"+(TEXT2HEX(v, l))))},
  {"label": "Text -> Octal", "method": lambda v, l: oct(int("0x"+(TEXT2HEX(v, l))))[2:]},
  {"label": "Text -> Binary", "method": lambda v, l: bin(int("0x"+TEXT2HEX(v, l)))[2:]},
  {"label": "Hexa -> Text", "method": HEX2TEXT},
  {"label": "Hexa -> Decimal", "method": lambda v, l: str(int("0x"+v))},
  {"label": "Hexa -> Octal", "method": lambda v, l: oct(int("0x"+v))[2:]},
  {"label": "Hexa -> Binary", "method": lambda v, l: bin(int("0x"+v))[2:]},
  {"label": "Decimal -> Text", "method": lambda v, l: HEX2TEXT(HEX(int(v), l), l)},
  {"label": "Decimal -> Hexa", "method": lambda v, l: HEX(int(v), l)},
  {"label": "Decimal -> Octal", "method": lambda v, l: oct(int(v))[2:]},
  {"label": "Decimal -> Binary", "method": lambda v, l: bin(int(v))[2:]},
  {"label": "Octal -> Text", "method": lambda v, l: HEX2TEXT(HEX(int("0o"+v), l), l)},
  {"label": "Octal -> Hexa", "method": lambda v, l: HEX(int("0o"+v), l)},
  {"label": "Octal -> Decimal", "method": lambda v, l: str(int("0o"+v))},
  {"label": "Octal -> Binary", "method": lambda v, l: bin(int("0o"+v))[2:]},
  {"label": "Binary -> Text", "method": lambda v, l: HEX2TEXT(HEX(int("0b"+v), l), l)},
  {"label": "Binary -> Hexa", "method": lambda v, l: HEX(int("0b"+v), l)},
  {"label": "Binary -> Decimal", "method": lambda v, l: str(int("0b"+v))},
  {"label": "Binary -> Octal", "method": lambda v, l: oct(int("0b"+v))[2:]},
]

def convert(mode:int, value:str, bytes:int=1) -> str:
  """Convert a unicode text or not to Hexa, Decimal, Octal or Binary, and vice versa."""

  if mode < 0 or mode > len(__CONVERT_MODES): raise IndexError("convertion mode not found. (>0, <{} available)".format(len(__CONVERT_MODES)))
  return '' if value == '' else __CONVERT_MODES[mode]["method"](value, bytes*2)

def print_modes(lines):
  def ljust(s, l): return s+' '*(l-len(s))
  best, l = 0, len(__CONVERT_MODES)
  max = len(str(l))+1
  for i in __CONVERT_MODES:
    if len(i["label"]) > best: best = len(i["label"])
  
  for i in range(lines): 
    print(ljust(str(i)+'-', max)+ljust(__CONVERT_MODES[i]["label"], best), '|', 
      ljust(str(i+lines)+'-', max)+__CONVERT_MODES[i+lines]["label"] if i+lines < l else '')
  del ljust, best, l, max, i

def demo(lines=13):
  print()
  print_modes(lines)

  try: m = int(input("\nMode: "))
  except ValueError as e:
    if not str(e).endswith("''"): print("Error: please type a number.")
    return  
  u = 2 if input("Unicode values? [y|N]: ").lower() == 'y' else 2
  v = input("Value: ")
  while v != '':
    print(convert(m, v, u))
    v = input("\nValue: ")

demo()