简体   繁体   English

Python-使用偏移量解码gsm 7位

[英]Python - decode gsm 7 bit with an offset

I am working on decoding gsm 7 bit and found this piece of code on this site from a user called noiam. 我正在对gsm 7位进行解码,并从一个名为noiam的用户的网站上找到了这段代码。

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)])

I have come to a crossroads in my decode as it doesn't help decode a message with an offset. 我在解码方面遇到了十字路口,因为它无助于解码带偏移量的消息。

If someone could help explain what that piece of code is doing and how to alter it to decode messages with an offset of 1/2/3/etc (both forwards and backwards) then you would be helping me out a lot. 如果有人可以帮助解释这段代码在做什么,以及如何对其进行更改以解码偏移量为1/2/3 / etc(向前和向后)的消息(向前和向后),那么您将对我有很大帮助。

Thanks in advance! 提前致谢!

It's string parsing in an exceptionally tight/efficient implementation. 它是一个非常紧凑/高效的实现中的字符串解析。 Below is what it could look like expanded out: 以下是展开后的样子:

'''
First Line: parse passed string:
For i initialized to 0, loop while i < length of f; step by 2 and assign i to
the reverse value in the list of values (so if len(f) = 10, i will be 
assigned to 8, 6, 4, 2, 0]
'''
f = ''
parsed_values = []
for i in range(0, len(f), 2)[::-1]: 
   # get substring of f from index i to i+2, [cast to base 16 int][3]
   temp = int(f[i:i+2], 16) 

   # convert the value "{0:08b}" to the value of temp and add it to the list
   parsed_values.append("{0:08b}".format(temp))

# finally, [stitch the parsed values together to make a single string][4]
f.join(parsed_values)

'''
Second Line:
Loop through values of parsed string; starting at index 0, step by 7, and
construct a return value out of the desired characters
'''
parsed_values = [] # reset parsed value array
for i in range(0, len(f), 7):
   # get the last char, the chunk (of length 7) that starts at i, and 
   # the last again. Cast to int base 2, convert to char
   temp = char(int(f[::-1][i:i+7][::-1], 2))

   # add to parsed value list
   parsed_values.append(temp)

return ''.join(parsed_values)

range docs: http://pythoncentral.io/pythons-range-function-explained/ 范围文档: http//pythoncentral.io/pythons-range-function-explained/

extended slice docs: https://docs.python.org/release/2.3.5/whatsnew/section-slices.html 扩展切片文档: https : //docs.python.org/release/2.3.5/whatsnew/section-slices.html

int docs: https://docs.python.org/2/library/functions.html#int 诠释文档: https : //docs.python.org/2/library/functions.html#int

join docs: https://docs.python.org/2/library/stdtypes.html#str.join 加入文档: https//docs.python.org/2/library/stdtypes.html#str.join

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM