Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. # turn hex values into decimals
  2. # and vise versa
  3. def hex_code(value, mode):
  4.  
  5.     # dictionary containing
  6.     # hexadecimal equivalents
  7.     # of decimals
  8.     hex_dict = {
  9.             10:'A',
  10.             11:'B',
  11.             12:'C',
  12.             13:'D',
  13.             14:'E',
  14.             15:'F'
  15.             }
  16.    
  17.     # if turning hex values
  18.     # into decimals, reverse
  19.     # the dictionary
  20.     if mode == 'decode':
  21.         hex_dict = {v:k for k,v in hex_dict.items()}
  22.  
  23.     # list the values so
  24.     # we can change their
  25.     # properties
  26.     value = list(value)
  27.  
  28.     for i in range(len(value)):
  29.         # store the current value
  30.         # so indexing will be easier
  31.         # to read
  32.         item = value[i]
  33.  
  34.         if not(item in hex_dict):
  35.             continue
  36.  
  37.         value[i] = hex_dict[item]
  38.  
  39.     return value
  40.  
  41. # any number base to decimal
  42. def any_to_decimal(value, from_base):
  43.  
  44.     # if value is in base 16,
  45.     # convert the values to
  46.     # decimal equivalent
  47.     value = hex_code(value, 'decode')
  48.  
  49.     # variables to store
  50.     # the output and the
  51.     # exponent
  52.     output = 0
  53.     expo  = 0
  54.  
  55.     # iterate through the
  56.     # value in reverse
  57.     for i in value[::-1]:
  58.         output += int(i) * (from_base ** expo)
  59.         expo += 1
  60.  
  61.     return output
  62.  
  63. # decimal to any number base
  64. def decimal_to_any(value, to_base):
  65.  
  66.     # list variable to
  67.     # store the remainders
  68.     output = []
  69.  
  70.     # append the remainders
  71.     # to output until value reaches 0
  72.     while value > 0:
  73.         output.append(value % to_base)
  74.         value //= to_base
  75.  
  76.     # convert values to hex
  77.     # equivalents if to_base == 16
  78.     if to_base == 16:
  79.         output = hex_code(output, 'encode')
  80.    
  81.     # reverse the output
  82.     output = output[::-1]
  83.  
  84.     return output
  85.  
  86. # octal to hexadecimal vise versa
  87. def octal_to_hexa(value, from_base):
  88.  
  89.     # turn the value into
  90.     # decimal so we can
  91.     # convert the decimal into
  92.     # the other base
  93.     output = any_to_decimal(value, from_base)
  94.  
  95.     # convert the value again to the opposite base
  96.     if from_base == 8:
  97.        output = decimal_to_any(output, 16)
  98.     else:
  99.        output = decimal_to_any(output, 8)
  100.  
  101.     return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement