Search code examples
pythonencodecodecgsm

decode 7-bit GSM


I found this post on how to encode ascii data to 7-bit GSM character set, how would I decode 7-bit GSM character again (reverse it back to ascii)?


Solution

  • For Python2:

    import binascii
    gsm = ("@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?"
           "¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ`¿abcdefghijklmnopqrstuvwxyzäöñüà")
    ext = ("````````````````````^```````````````````{}`````\\````````````[~]`"
           "|````````````````````````````````````€``````````````````````````")
    
    def gsm_encode(plaintext):
        result = []
        for c in plaintext:
            idx = gsm.find(c)
            if idx != -1:
                result.append(chr(idx))
                continue
            idx = ext.find(c)
            if idx != -1:
                result.append(chr(27) + chr(idx))
        return ''.join(result).encode('hex')
    
    def gsm_decode(hexstr):
        res = hexstr.decode('hex')
        res = iter(res)
        result = []
        for c in res:
            if c == chr(27):
                c = next(res)
                result.append(ext[ord(c)])
            else:
                result.append(gsm[ord(c)])
        return ''.join(result)
    
    code = gsm_encode("Hello World {}")
    print(code)
    # 64868d8d903a7390938d853a1b281b29
    print(gsm_decode(code))
    # Hello World {}