使用Python在十六进制转换函数中出现16错误

时间:2012-10-29 11:08:10

标签: python hex tornado

大家好我在Web服务器Tornado中用Python编写了这部分代码:

# Transform an integer into a string with HEX symbols in the format that is
# understandable by Quantaservd
def hextransform(data, length):
    data = hex(data)[2:]
    assert(len(data) <= length)
    # zero-padding
    data = ('0' * (length - len(data))) + 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 hex2char(i):
    return ('0'+hex(i)[2:])[-2:]

def cmd_payload(op1_hex, op2_hex, l, pl, ck):
    return hex2char(op1_hex) + hex2char(op2_hex) + hex2char(l) + pl + hex2char(ck)

def packet2json(op_group, op_code, payload): 

    lun = len(payload) / 2
    fcs = op_group ^ op_code ^ lun #fcs = XOR of op_group, 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

def invert2bytes(stringa, ind):
    byte_low = stringa[ind:ind + 2]
    ind += 2
    byte_high = stringa[ind:ind + 2]
    return byte_high + byte_low

我有这个错误:

File "./wsn.py", line 883, in get cmdjson = packet2json(op_group_hex,op_code_hex, packet)
File "./wsn.py", line 180, in packet2json fcs = fcs ^ int('0x' + payload[(i - 1):(i + 1)], 16)
ValueError: invalid literal for int() with base 19: '0x*`0'

当程序到达hextransform函数时....我不知道这是否是一个基本错误,因为在错误中它说的是关于base 19的内容,mu函数是在16中。或者如果问题是'*'字符......

有什么想法吗?非常感谢你。

修改

我添加了调用hextransform函数的处理程序中的这部分代码:

#Command OnOffCmd_SetState
    if command in ['ON', 'OFF', 'TOGGLE']:
        op_group = "70"
        op_code = "50"
        packet_meta = "*%s;%s;%s;%s;02%s%s600080000%s#"
        pkt_len = hextransform(16, 2)
        netid = hextransform(int(nid), 16)
        sens_id = hextransform(int(sid) >> 16, 4)
        sens_id_little = invert2bytes(sens_id,0)
        cluster_id = hextransform(int(sid) & 0x00FFFF, 4)
        end_point = "08"


        if command == 'ON':
            cmd_data = "01"
        elif command == 'OFF':
            cmd_data = "00"
        elif command == 'TOGGLE':
            cmd_data = "02"

        packet = packet_meta % (netid, op_group, op_code, pkt_len, sens_id, end_point, cmd_data)
        packet = packet.upper()

        op_group_hex=0x70
        op_code_hex=0x50

        cmdjson = packet2json(op_group_hex,op_code_hex, packet)


    #Command ZDP-RestartNwk.Request
    elif command == 'RESTARTNWK':
        op_group = "A3"
        op_code = "E0"
        netid = hextransform(int(nid), 16)
        packet_meta = "*%s;%s;%s;%s;02600080000#"
        pkt_len = hextransform(0, 1)

        packet = packet_meta % (netid, 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)

    #Command ZDP-IEEE_addr.Request            
    elif command == 'IDENTIFY':
        op_group = "A2"
        op_code = "05"
        packet_meta = "*%s;%s;%s;%s;02%s600080000#"
        pkt_len = hextransform(2, 1)
        netid = hextransform(int(nid), 16)
        sens_id = hextransform(int(sid) >> 16, 4)
        sens_id_little = invert2bytes(sens_id,0)

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

        op_group_hex=0xA2
        op_code_hex=0x05

        cmdjson = packet2json(op_group_hex,op_code_hex, packet)

编辑2

如果我写了

pkt_len = hextransform(2, 1)

我有错误。不是,如果我有

pkt_len = hextransform(16, 2)

1 个答案:

答案 0 :(得分:0)

阅读this answer。它有关于何时使用16与0的基数的想法。

当你声明你正在使用base 16时(正如你所做的那样),我不会包含0x - 这是表示十六进制数的特殊语法,但实际上并不是十六进制数。

所以,要么:

  1. 删除0x前缀或
  2. 使用0而不是16
  3. 的基数

    如果这些都不起作用,那么您的有效负载字符串就会出现问题。它可能包含您不期望的字符。