【发布时间】:2013-08-17 19:42:19
【问题描述】:
我有一个解析器,它读取一个长八位字节字符串,我希望它根据解析细节打印出更小的字符串。它读取一个十六进制字符串,如下所示
字符串的格式如下:
01046574683001000004677265300000000266010000
十六进制中包含的接口格式如下:
version:length_of_name:name:op_status:priority:reserved_byte
==
01:04:65746830:01:00:00
==(从十六进制转换时)
01:04:eth0:01:00:00
^ 这是字符串的 1 段,代表 eth0(我插入了 : 以便于阅读)。然而,此刻我的代码返回一个空白列表,我不知道为什么。谁能帮帮我!
def octetChop(long_hexstring, from_ssh_):
startpoint_of_interface_def=0
# As of 14/8/13 , the network operator has not been implemented
network_operator_implemented=False
version_has_been_read = False
position_of_interface=0
chopped_octet_list = []
#This while loop moves through the string of the interface, based on the full length of the container
try:
while startpoint_of_interface_def < len(long_hexstring):
if version_has_been_read == True:
pass
else:
if startpoint_of_interface_def == 0:
startpoint_of_interface_def = startpoint_of_interface_def + 2
version_has_been_read = True
endpoint_of_interface_def = startpoint_of_interface_def+2
length_of_interface_name = long_hexstring[startpoint_of_interface_def:endpoint_of_interface_def]
length_of_interface_name_in_bytes = int(length_of_interface_name) * 2 #multiply by 2 because its calculating bytes
end_of_interface_name_point = endpoint_of_interface_def + length_of_interface_name_in_bytes
hex_name = long_hexstring[endpoint_of_interface_def:end_of_interface_name_point]
text_name = hex_name.decode("hex")
print "the text_name is " + text_name
operational_status_hex = long_hexstring[end_of_interface_name_point:end_of_interface_name_point+2]
startpoint_of_priority = end_of_interface_name_point+2
priority_hex = long_hexstring[startpoint_of_priority:startpoint_of_priority+2]
#Skip the reserved byte
network_operator_length_startpoint = startpoint_of_priority+4
single_interface_string = long_hexstring[startpoint_of_interface_def:startpoint_of_priority+4]
print single_interface_string + " is chopped from the octet string"# - keep for possible debugging
startpoint_of_interface_def = startpoint_of_priority+4
if network_operator_implemented == True:
network_operator_length = long_hexstring[network_operator_length_startpoint:network_operator_length_startpoint+2]
network_operator_length = int(network_operator_length) * 2
network_operator_start_point = network_operator_length_startpoint+2
network_operator_end_point = network_operator_start_point + network_operator_length
network_operator = long_hexstring[network_operator_start_point:network_operator_end_point]
#
single_interface_string = long_hexstring[startpoint_of_interface_def:network_operator_end_point]
#set the next startpoint if there is one
startpoint_of_interface_def = network_operator_end_point+1
else:
self.network_operator = None
print single_interface_string + " is chopped from the octet string"# - keep for possible debugging
#This is where each individual interface is stored, in a list for comparison.
chopped_octet_list.append(single_interface_string)
finally:
return chopped_octet_list
【问题讨论】:
-
您的代码似乎过于复杂。当没有要捕获的异常时,为什么会有
try块?为什么你有一个从未使用过的from_ssh_参数?当这个解析任务似乎不需要迭代时,为什么你有一个带有布尔值的 while 循环来检查循环中的状态?您可以一次计算所有指数。为什么输入字符串包含一堆未在输出字符串中显示的额外数字?所有这些问题都使得破译您的问题和代码变得非常困难。我想帮助你,但请先澄清你的问题/代码。