是否有用于将ascii数据编码为7位GSM字符集(用于发送SMS)的python库?
# -*- coding: utf8 -*- gsm = (u"@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>" u"?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà") ext = (u"````````````````````^```````````````````{}`````\\````````````[~]`" u"|````````````````````````````````````€``````````````````````````") def gsm_encode(plaintext): res = "" for c in plaintext: idx = gsm.find(c) if idx != -1: res += chr(idx) continue idx = ext.find(c) if idx != -1: res += chr(27) + chr(idx) return res.encode('hex') print gsm_encode(u"Hello World")
输出为十六进制。显然,如果需要二进制流,则可以跳过
# -*- coding: utf8 -*- import binascii gsm = ("@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?" "¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà") ext = ("````````````````````^```````````````````{}`````\\````````````[~]`" "|````````````````````````````````````€``````````````````````````") def gsm_encode(plaintext): res = "" for c in plaintext: idx = gsm.find(c); if idx != -1: res += chr(idx) continue idx = ext.find(c) if idx != -1: res += chr(27) + chr(idx) return binascii.b2a_hex(res.encode('utf-8')) print(gsm_encode("Hello World"))