repl.it
@xMikee/

FBZ Encoding

Python

"Foo-bar-baz" encoding. A simple way to encode a string with a bunch of "foo-bar-baz"-es.

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import textwrap, re, os

try:
  def translate(string, trans_dict):
    for x in trans_dict.items():
      if x[0] in string:
        string = string.replace(x[0], x[1])
    return string

  def fooencode(st):
    st = st[::-1]
    binary = ' '.join(format(ord(x), 'b') for x in st)
    binary = binary.replace('00', 'fo')
    binary = binary.replace('01', 'foo')
    binary = binary.replace('10', 'br')
    binary = binary.replace('11', 'bar')
    binary = binary.replace('0', 'bz')
    binary = binary.replace('1', 'baz')
    return binary

  def foodecode(binary):
    binary = re.sub('fo(?!o)', '00', binary)
    binary = binary.replace('foo', '01')
    binary = binary.replace('br', '10')
    binary = binary.replace('bar', '11')
    binary = binary.replace('bz', '0')
    binary = binary.replace('baz', '1')
    decoded = ''.join([chr(int(x, 2)) for x in binary.split()])
    return decoded[::-1]

  f = fooencode(input('\033[91mEnter string >> \033[0m'))
  print()
  rows, cols = os.popen('stty size', 'r').read().split()
  print(textwrap.fill("\033[91mFoo-encoded string:\033[0m " + f, int(cols)))
  print("\n\033[91mString decoded from foo-encoding:\033[0m",foodecode(f))
except KeyboardInterrupt:
  print('Control-c happened; this is for testing purposes for a bug. If you see this after a control-c, good! That means the bug was fixed.')