python base100

复制代码
def encode(data, encoding="utf-8"):
    """
    Encodes text to emoji
    :param data: Original text as bytes array or plaintext
    :param encoding: (Optional) encoding if {data} passed as plaintext
    :return: bytes array of encoded text
    """
    if isinstance(data, str):
        data = data.encode(encoding)
    out = [240, 159, 0, 0]*len(data)
    for i, b in enumerate(data):
        out[4*i+2] = (b + 55) // 64 + 143
        out[4*i+3] = (b + 55) % 64 + 128
    return bytes(out)


def decode(data, encoding="utf-8"):
    """
    Decodes emoji to text
    :param data: Encoded text in form of emoji as bytes array or plaintext
    :param encoding: (Optional) encoding if {data} passed as plaintext
    :return: bytes array of decoded text
    """
    if isinstance(data, str):
        data = data.encode(encoding)
    if len(data) % 4 != 0:
        raise Exception('Length of string should be divisible by 4')
    tmp = 0
    out = [None]*(len(data) // 4)
    for i, b in enumerate(data):
        if i % 4 == 2:
            tmp = ((b - 143) * 64) % 256
        elif i % 4 == 3:
            out[i//4] = (b - 128 + tmp - 55) & 0xff
    return bytes(out)


string = "Hello World!"
encoded = encode(string) # Encode the string
print(encoded.decode())
decoded = decode(encoded) # Decode the encoded string
print(decoded.decode())

复制代码

 

posted @   Maple_feng  阅读(177)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示