0

Hi all I have this part of code in Python in the web server Tornado:

# 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

I have this error:

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'

when the program arrives at hextransform function.... I don't know if this is a base error, because in the error it says something about base 19, and mu function is in base 16. Or if the problem is the '*' character...

Any ideas? Thank you very much.

EDIT

I add this part of code that is in an handler that calls the hextransform function:

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

EDIT 2

If I wrote

pkt_len = hextransform(2, 1)

I have the error. Not if I have

pkt_len = hextransform(16, 2)
sharkbait
  • 2,980
  • 16
  • 51
  • 89
  • 1
    Please strip your code down to an [SSCCE](http://sscce.org). We're not going to read all of that (incorrectly indented) code. – Fred Foo Oct 29 '12 at 11:13
  • Seems there are at least two impossibilities with this code: (1) You have a 5 character literal passed to int(), but a '0x' + any [i-1:i+1] slice should be at most 4 characters. (2) Python doesn't change arguments to functions at random... Are you sure you're showing us the right code here? – Kenan Banks Oct 29 '12 at 11:22
  • 2
    That's a lot better. I must say, I'm baffled by the "base 19" in the error message as you're passing literal 16. But in any case, `payload[(i - 1):(i + 1)]` does not contain what you think it does; try printing it prior to converting to `int`. – Fred Foo Oct 29 '12 at 11:24
  • Sorry can you write me in an answer in that way I have to write this code? In don't understand very well.. – sharkbait Oct 29 '12 at 11:39
  • Looks like `payload` is not hex. – Janne Karila Oct 29 '12 at 11:59
  • yeah I edit the code now so you can see. – sharkbait Oct 29 '12 at 12:05
  • I think the error is in `pkt_len = hextransform(2, 1)` and the other statements – sharkbait Oct 29 '12 at 12:16

1 Answers1

0

Have a read through this answer. It has thoughts on when to use a base of 16 versus 0.

When you state that you are using base 16 (as you are doing), I would not include the 0x -- that is special syntax that denotes a hexadecimal number but is not actually hex itself.

So, either:

  1. Remove the 0x prefix, or
  2. Use a base of 0 instead of 16

If neither of these works, then you have a problem with your payload string. It may contain characters that you didn't expect.

Community
  • 1
  • 1
paddy
  • 60,864
  • 6
  • 61
  • 103