Python library for converting plain text (ASCII) into GSM 7-bit character set?

前端 未结 4 759
梦如初夏
梦如初夏 2020-12-16 21:17

Is there a python library for encoding ascii data to 7-bit GSM character set (for sending SMS)?

4条回答
  •  萌比男神i
    2020-12-16 21:22

    I faced a similar issue recently where we were getting gsm7bit decoded text messages, mostly for Verizon carrier with Spanish characters, from the aggregator and we were not able to decode it successfully. Here is the one I created with the help of other answers in the forum. This is for Python 2.7.x.

    def gsm7bitdecode(text):
        gsm = (u"@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>"
               u"?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ`¿abcdefghijklmnopqrstuvwxyzäöñüà")
        ext = (u"````````````````````^```````````````````{}`````\\````````````[~]`"
               u"|````````````````````````````````````€``````````````````````````")
    
        text = ''.join(["{0:08b}".format(int(text[i:i+2], 16)) for i in range(0, len(text), 2)][::-1])
    
        text = [(int(text[::-1][i:i+7][::-1], 2)) for i in range(0, len(text), 7)]
        text = text[:len(text)-1] if text[-1] == 0 else text
        text =iter(text)
    
        result = []
        for i in text:
            if i == 27:
                i = next(text)
                result.append(ext[i])
            else:
                result.append(gsm[i])
    
        return "".join(result).rstrip()
    
    

提交回复
热议问题