Python-使用偏移量解码gsm 7位
我正在解码gsm 7位,在这个网站上从一个叫noiam的用户那里找到了这段代码Python-使用偏移量解码gsm 7位,python,sms,offset,gsm,7-bit,Python,Sms,Offset,Gsm,7 Bit,我正在解码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)]) 我在解码过程中遇到了一个
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/等的消息(向前和向后),那么你会帮我很多忙
提前谢谢 这是一个非常紧凑/高效的字符串解析实现。下面是它的外观:
'''
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)
“”
第一行:解析传递的字符串:
对于初始化为0的i,在i
范围文档:
扩展切片文档:
int文档:
join docs:这是一个非常紧凑/高效的字符串解析实现。下面是它的外观:
'''
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)
“”
第一行:解析传递的字符串:
对于初始化为0的i,在i
范围文档:
扩展切片文档:
int文档:
加入文档: