sam1902

Zero Width Encoder

Apr 4th, 2018
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. from sys import argv
  2. if len(argv) <= 1:
  3.     exit("Synthax:\npython3 script.py string to encode goes here, spaces are allowed")
  4.  
  5. def textToBits(text, encoding='utf-8', errors='surrogatepass'):
  6.     bits = bin(int.from_bytes(text.encode(encoding, errors), 'big'))[2:]
  7.     return bits.zfill(8 * ((len(bits) + 7) // 8))
  8.  
  9. # Used to separate bits
  10. #zws = "\u200b" # Zero width space
  11. # Represents a one
  12. zwj = "\u200d" # Zero width joiner
  13. #zwj = "a"
  14. # Represents a zero
  15. zwnj = "\u200c" # Zero width non-joiner
  16. #zwnj = "b"
  17.  
  18. message = ""
  19. for arg in argv[1:]:
  20.     message += arg + " "
  21. message = message[:-1]
  22.  
  23. msgBin = textToBits(message)
  24.  
  25. print("Original message: " + message)
  26. #print("Binary message: " + msgBin)
  27.  
  28. msgZw = "".join([zwj if bit in "1" else zwnj for bit in msgBin])
  29. print("\t==> Zero width message can be hard to copy from some terminal, they have been outputted to a text file along with instructions")
  30. print("Zero Width message (len: {}): \"{}\"".format(len(msgZw), msgZw))
  31. with open("zeroWidth.txt", "w") as txtFile:
  32.         txtFile.write("vvvv COPY BELOW ({} chars) vvvv\n".format(len(msgZw)) + msgZw + "\n^^^^ COPY ABOVE ^^^^")
  33. print("Zero width message has been saved to 'zeroWidth.txt'")
Advertisement
Add Comment
Please, Sign In to add comment