解码7位GSM

2024-05-29 01:34:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现了post关于如何将ascii数据编码为7位GSM字符集,我将如何再次解码7位GSM字符(将其反转回ascii)?


Tags: ascii解码字符postgsm字符集数据编码
3条回答

对于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 {}

例如:

C7F7FBCC2E03代表“谷歌”
Python3.4

def gsm7bitdecode(f):
   f = ''.join(["{0:08b}".format(int(f[i:i+2], 16)) for i in range(0, len(f), 2)][::-1])
   return ''.join([chr(int(f[::-1][i:i+7][::-1], 2)) for i in range(0, len(f), 7)])

打印(gsm7bitdecode('C7F7FBCC2E03'))

Google

有一个非常简单的解决方案:

转换二进制八位字节中的十六进制将每个八位字节放在一个数组中,但顺序相反(整个八位字节,而不是位),因为这是它们的发送方式。按7位组从右到左读取字符串数字是GSM 7位表中的字符代码

例如:

C7F7FBCC2E03代表“谷歌”

按相反顺序排列的字符串是

03-2E-CC-FB-F7-C7

六个八位元是

00000011-00101110-11001100-111111011-11110111-11000111

隔膜是

000000-1100101-1101100-1100111-1101111-1101111-1000111

从右到左依次为:

GSM 7bit表中的七位十进制valor字符

1000111-71-G号

1101111-111-o号

1101111-111-o号

1100111-103-g号

1101100-108-l号

1100101-101-e号

放弃最后的0000000值

相关问题 更多 >

    热门问题