'Custom encoding and decoding UTF special characters

Just for fun I've been embedding text in images. The following code is a distillation and demonstration of the encoding and decoding mechanism I am using.

class encChar:
    def __init__(self,char):
        self.p = self.enc(char)
    def enc(self,char):
        d = bin(ord(char)).split('b')[1]
        while len(d)<8:
            d = "0"+d
        rdif = int(d[0])*4 + int(d[1])*2 + int(d[2])*1
        gdif = int(d[3])*2 + int(d[4])*1
        bdif = int(d[5])*4 + int(d[6])*2 + int(d[7])*1
        return (rdif,gdif,bdif)
    def dec(self):
        dmap = {0:"000",1:"001",2:"010",3:"011",4:"100",5:"101",6:"110",7:"111"}
        r = dmap[self.p[0]]
        g = dmap[self.p[1]][1:]
        b = dmap[self.p[2]]
        n = int(r+g+b,2)
        return chr(int(r+g+b,2))

testStr = """Languages
Deutsch
Español
Français
한국어
Italiano
Русский
Tagalog
Tiếng Việt
中文"""

result= ""
for line in testStr.split("\n"):

    result+=line+"\n"
    print(line)
    print("".join([encChar(k).dec() for k in line]))
    result+="".join([encChar(k).dec() for k in line])+"\n"
    print()
    result+="\n"

with open("errorop.txt","w",encoding="utf8") as op:
    op.write(result)

Which produces the following document:

Languages
Languages

Deutsch
Deutsch

Español
Español

Français
Français

한국어
Õ­Å

Italiano
Italiano

Русский


Tagalog
Tagalog

Tiếng Việt
Tiõng Viöt

中文
Ë

As you can see several runes are altered by the process and I'm wondering how I can preserve them through this process.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source