Advertisement
paster442

Encoding/Decoding

May 19th, 2021 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. Encoding:
  2.  
  3. [code py]
  4. chars = ['', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '.', ' ', '_']
  5.  
  6.  
  7. def encode (val):
  8.     letternum = 1
  9.     val = str(val)
  10.     encoded = ""
  11.    
  12.     for i in range (1, len(str(val))):
  13.        encoded = str(encoded) + str(chars.index(val[letternum-1]))
  14.        letternum += 1
  15.  
  16.  
  17.     return int(encoded + "00")
  18.  
  19.  
  20. print(encode(input("Enter a word: ")))
  21. [/code]
  22.  
  23.  
  24. Decoding:
  25.  
  26. [code py]
  27. chars = ['', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '.', ' ', '_']
  28.  
  29.  
  30. def decode (val):
  31.     letternum = 1
  32.     value = ""
  33.     idx = None
  34.  
  35.     while True:
  36.         val = str(val)
  37.         idx = val[letternum-1] + val[letternum]
  38.         letternum += 2
  39.         if int(idx) < 1:
  40.             break
  41.         value = value + chars[int(idx) - 1]
  42.     return value
  43.  
  44.  
  45. print(decode(input("Enter an encoded value: ")))
  46. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement