cov.py

Created by polettielio

Created on April 06, 2023

3.6 KB

Another smarter numeral base converter


### cov.py v3
# chglogs:
# v2 correct dec() support even with bases > 36
# v3 dec() now uses a table of ints

def dec(m, b, s=False, p=False):
  ### dec - convert to base 10
  # 
  # NEEDED arguments:
  # m: str/int/list/tuple
  #    number in entered base
  #    !!! must contain ONLY digits (no a/b/c/etc)
  #    => use tuples/lists for bases bigger than 10
  #       ex: 1AC base 16 -> [1, 10, 12]
  #    dec() automatically converts values if receives correct ones
  # b:int, base to convert from
  # OPTIONNAL arguments:
  # s:bool, True means silent mode
  # p:bool, porcelene mode, don't use manually
  #    True to return table, False to print it
  # porcelene mode returns a ttuple:
  #  - the sum (in base 10) of the
  #    gotten nb in base 10
  #  - an int containing the base to convert to (always 10)
  #  - ex: dec(20,16) => ([1,0], 10) (for 32)
  n=[]
  if type(m) == int:
    m=str(m)
  for i in range(len(m)):
    n.append(int(m[i]))
  if not s:
    print("table", n, "\n")
  o=0
  for i in range(len(n)):
    if n[i] < b:
      v=n[i] * (b ** (len(n)-1-i))
      o+=v
      if not s:
        print("+", n[i], "x", str(b) + "^" + str((len(n)-1-i)), "( = " + str(v) + ")")
    else:
      print("err/dec:", n[i], ">=", b)
      return
  print()
  print(o)
  if p:
    return (o, b)

def cov(n, b=16, i=True, s=False, p=False):
  ### cov - convert from base 10 to others
  #
  # NEEDED arguments:
  # n:int, number in base 10
  # OPTIONNAL arguments:
  # b:int, base to convert to (default 16)
  # i:bool, intelligent, aka convert table to real hex
  #    (if happens to be hex)
  # s:bool, True means silent mode
  # p:bool, porcelene mode, don't use manually
  #    True to return table, False to print it
  # porcelene mode returns a ttuple:
  #  - a table w/ digits (in base 10) of the
  #    gotten nb in the wanted base
  #  - an int containing the base to convert to
  #    aka th ebase of the nb in the table
  #  - ex: cov(32) => ([2,0], 16) (for 0x20)
  n=int(n)
  rest=1
  out=[]
  if not s:
    print("base", b,  "\n")
  while n > 0:
    rest=n%b
    n//=b
    out.insert(0, rest)
    if not s:
      print(n, "x", b, "+", rest)
  if b == 16 and not s:
    print(str(out) + "." + str(b))
    for i in range(len(out)):
      out[i] = hex(out[i]).split('x')[-1]
    print()
    print("hex (base 16):", "".join(map(str,out)))
  elif b < 10 and not s:
    print(str(out) + "." + str(b))
    print()
    print("base", str(b) + ":", "".join(map(str,out)))
  else:
    print()
    print(str(out) + "." + str(b))
  if p:
    return (out, b)

def aide(t="none"):
  if t == "none":
    print("liste des commandes :")
    print("(exécuter la commande donnée")
    print("pour l'aide)")
    print(" cov() : aide('cov')")
    print(" dec() : aide('dec')")
  if t == "cov":
    print("cov(n, b)")
    print(" calcule n, de base 10 vers b")
    print(" n et b peuvent être int")
    print("  ou string")
    print(" ex: 12 (base 10) => base 8")
    print("  cov(12, 8)")
    print("  cov('12', 8)")
  if t == "dec":
    print("dec(n, b)")
    print(" calcule n, de base b vers 10")
    print(" n et b peuvent être int")
    print("  ou string")
    print(" n peut être liste/tuple :")
    print("  chaque item est un chiffre")
    print("  donné en base 10")
    print()
    input("[appuyer sur exe]")
    print()
    print(" ex: 12 (base 16) => base 10")
    print("  dec(12, 16)")
    print("  dec([1, 2], 16)")
    print("  dec(('1', 2), '16')")
    print(" ex: 1A (base 16) => base 10")
    print("  note! dec() ne connait pas")
    print("   les lettres")
    print("  dec([1, 10],16) # car A=10")

print("afficher de l'aide:")
print(" exécuter aide()")