Advertisement
DeaD_EyE

Sexadecimal/Hexadecimal encode/decode

Sep 7th, 2021
1,460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. def decode_sex(sex_value: str) -> str:
  2.     """
  3.    Decode from Sexadecimal to Hexadecimal
  4.    """
  5.     minus = "-"
  6.     if sex_value[0] == minus:
  7.         sex_value = sex_value[1:]
  8.         negative = True
  9.     else:
  10.         negative = False
  11.        
  12.     digits = "0123456789KSNJFL"
  13.     result = 0
  14.     for index, digit in enumerate(reversed(sex_value)):
  15.         result += digits.index(digit.upper()) * 16 ** index
  16.     if negative:
  17.         result *= -1
  18.     return f"{result:x}"
  19.  
  20.  
  21. def encode_sex(hex_value: str) -> str:
  22.     """
  23.    Encode Hexadecimal to Sexadecimal
  24.    """
  25.     minus = "-"
  26.    
  27.     if hex_value[0] == minus:
  28.         hex_value = hex_value[1:]
  29.         prefix = minus
  30.     else:
  31.         prefix = ""
  32.  
  33.     result = []
  34.     digits = "0123456789KSNJFL"
  35.     value = int(hex_value, 16)
  36.     rest = 0
  37.     while value:
  38.         result.append(digits[value % 16])
  39.         value //= 16
  40.     return prefix + "".join(reversed(result))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement