# ==============================================================================
"""BINHEX : show decimal, binary and hexadecimal representations for integers"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0"
__date__    = "2022-11-12"
__usage__   = """
User input : <value> [value...] where value:int is given in decimal
App output : show decimal, binary and hexa representation for all values"""
# ==============================================================================
from ezCLI import *
# ------------------------------------------------------------------------------
def dec2bin(value:int) -> str:
  """return the binary representation for a (decimal) integer"""
  digits, binvalue = '01', [] if value else ['0']; #inspect()
  while value:
    binvalue.append(digits[value % 2])
    value //= 2; #inspect()
  binvalue.reverse(); #inspect()
  return '0B' + ''.join(binvalue)
# ,------------------------------------------------------------------------------
def dec2hex(value:int) -> str:
  """return the hexadecimal representation for a (decimal) integer"""
  digits, hexvalue = '0123456789ABCDEF', [] if value else ['0']; #inspect()
  while value:
    hexvalue.append(digits[value % 16])
    value //= 16; #inspect()
  hexvalue.reverse(); #inspect()
  return '0X' + ''.join(hexvalue)
# ------------------------------------------------------------------------------
def parser(command:str) -> str:
  """parse 'command' into integers and apply binary/hexa conversion on them"""
  values = parse(command); #inspect()
  if type(values) is tuple:
    isinteger = [type(value) is int for value in values]; #inspect()
    assert all(isinteger), "all values must be integers"
    return '\n'.join(f"{n} = {dec2bin(n)} = {dec2hex(n)}" for n in values)
  else:
    assert type(values) is int, "all values must be integers"
    return f"{values} = {dec2bin(values)} = {dec2hex(values)}\n"
# ==============================================================================
if __name__ == "__main__":
  userloop(parser, "Enter <value> [value...]")
# ==============================================================================
