Python中的十六进制转换错误

0 投票
1 回答
612 浏览
提问于 2025-04-17 16:23

我遇到了一个问题。

我有一个页面,可以向传感器网络发送命令。

当我点击这段代码时:

<a href='javascript:void(send_command_to_network("{{net.id}}", "restartnwk"));'>Restart Network <i class="icon-repeat"></i> </a>

我会调用一个JavaScript函数,具体是:

function send_command_to_network(net, command) {
    $.ajax({url: "/networks/" + net + "/send?command=" + command,
    type: "GET",
    async: true,
    dataType: "json",
    success: function(json_response) { 
         var err = json_response['error'];
     if (err) {
       show_alert('error', err);
       return;
     }
     var success = json_response['success'];
     if (success) {
       show_alert('success', success);
       return;
     }
     show_alert('alert', "This should not happen!");
       }
     }); 
    }

这个函数会生成一个网址,用来调用一个在Tornado这个用Python写的网页服务器里的处理程序。这个处理程序是:

class NetworkSendHandler(BaseHandler):
    # Requires authentication 
    @tornado.web.authenticated
    def get(self, nid):
        # Get the command 
        command = self.get_argument('command').upper(); 

        # The dictionary to return
        ret = {}

        #Check if the command is available
        if command not in ['RESTARTNWK']:
            raise tornado.web.HTTPError(404, "Unknown command: " + str(command))


        #Command ZDP-RestartNwk.Request
        if command == 'RESTARTNWK':
            op_group = "A3"
            op_code = "E0"
            packet_meta = "*%s;%s;%s;#"
            pkt_len = hextransform(16, 2)

            packet = packet_meta % (op_group, op_code, pkt_len)
            packet = packet.upper()

            op_group_hex=0xA3
            op_code_hex=0xE0

            cmdjson = packet2json(op_group_hex,op_code_hex, packet)

        self.finish()

在Tornado的调试控制台里,我收到了这个错误:

Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/tornado/web.py", line 988, in _execute
    getattr(self, self.request.method.lower())(*args, **kwargs)
  File "/usr/lib/python2.6/site-packages/tornado/web.py", line 1739, in wrapper
    return method(self, *args, **kwargs)
  File "./wsn.py", line 859, in get
    cmdjson = packet2json(op_group_hex,op_code_hex, packet)
  File "./wsn.py", line 188, in packet2json
    fcs = fcs ^ int('0x' + payload[(i - 1):(i + 1)], 16)
ValueError: invalid literal for int() with base 16: '0x*A'

我觉得错误出现在调用packet2json之后。我认为这个错误是在函数内部的转换过程中,因为错误信息说'0x*A'不是一个有效的整数。我觉得这个'*'就是问题所在……我该怎么解决这个问题呢?

补充说明

抱歉,我忘记了函数的内容:

# Transform an integer into a string with HEX symbols in the format that is
# understandable by Quantaservd
def hextransform(data, length):
    data = hex(data).lstrip('0x').rstrip('L')
    assert(len(data) <= length)
    # zero-padding
    data = ('0' * (length - len(data))) + data
    print data
    # Swap 'bytes' in the network ID
    data = list(data)
    for i in range(0, length, 2):
        tmp = data[i]
        data[i] = data[i + 1]
        data[i + 1] = tmp
    # Reverse the whole string (TODO: CHECK) 
    data.reverse()
    data = "".join(data)
    return data


def packet2json(op_group, op_code, payload): 
    #op_group =
    #op_code =
    #payload=""    # stringa ascii 2 char per byte, senza checksum 
    lun = len(payload) / 2
    fcs = op_group ^ op_code ^ lun #fcs = XOR of op_groip, op_code, lenght and  payload
    for i in range(len(payload)):
    if ((i % 2) == 1):
        fcs = fcs ^ int('0x' + payload[(i - 1):(i + 1)], 16)
        print int('0x' + payload[(i - 1):(i + 1)], 16)
    s=cmd_payload(op_group,op_code, lun, payload, fcs)
    #p = r '{"data": "' + s + r '"}'
    p=r'{"data": "'+s+r'"}'
    return p

1 个回答

0

问题出在 packet2json() 这个函数上:

for i in range(len(payload)):
    if ((i % 2) == 1):
        fcs = fcs ^ int('0x' + payload[(i - 1):(i + 1)], 16)

在你的程序中,你是这样调用这个函数的:

packet2json(0xa3, 0xe0, '*A3;E0;10;#')

现在上面的循环会从第一个字符开始(索引为0),因为它的索引不是奇数,所以会被忽略。接着会检查第二个字符(索引为1),它的索引是奇数(1%2 == 1)。在 if 语句的内容中,使用了 int('0x' + payload[(i-1):(i+1)], 16),这实际上是 int('0x' + payload[0:2], 16),也就是 int('0x*A',16),这会导致程序崩溃...

你需要重新写这个 packet2json() 函数,使用正确的字符串索引,或者在调用时确保负载中没有前面的 *

撰写回答