簡體   English   中英

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

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

我正在對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)])

我在解碼方面遇到了十字路口,因為它無助於解碼帶偏移量的消息。

如果有人可以幫助解釋這段代碼在做什么,以及如何對其進行更改以解碼偏移量為1/2/3 / etc(向前和向后)的消息(向前和向后),那么您將對我有很大幫助。

提前致謝!

它是一個非常緊湊/高效的實現中的字符串解析。 以下是展開后的樣子:

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

范圍文檔: http//pythoncentral.io/pythons-range-function-explained/

擴展切片文檔: https : //docs.python.org/release/2.3.5/whatsnew/section-slices.html

詮釋文檔: https : //docs.python.org/2/library/functions.html#int

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM