diff --git a/src/tw_packet_decoder.py b/src/tw_packet_decoder.py index 29842db..a2c45c8 100644 --- a/src/tw_packet_decoder.py +++ b/src/tw_packet_decoder.py @@ -18,6 +18,7 @@ def str_to_bytes(data: str) -> bytes: return ast.literal_eval(data) data = re.sub(r',\s*0x', '', data) + data = re.sub(r',\s*$', '', data) if re.match('^0x', data): data = data.replace('0x', '') diff --git a/tests/hexstr_to_annotation_test.py b/tests/hexstr_to_annotation_test.py index 2ddcfe3..67a1d4c 100644 --- a/tests/hexstr_to_annotation_test.py +++ b/tests/hexstr_to_annotation_test.py @@ -34,6 +34,39 @@ def test_comma_0xhex(): print(info) assert info['bytes'] == '00 01 ff' + +def test_comma_0xhex_long(): + data = """ + 0x3a, 0x01, 0x05, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x5f, 0x70, 0x69, 0x63, 0x6b, 0x75, 0x70 + """ + info: HexInfo = hex_str_to_annotation(data, protocol_6=True, protocol_7=True) + print(info) + assert info['bytes'] == '3a 01 05 62 72 69 64 67 65 5f 70 69 63 6b 75 70' + +def test_comma_0xhex_trailing_comma(): + data = """ + 0x3a, 0x01, 0x05, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x5f, 0x70, 0x69, 0x63, 0x6b, 0x75, 0x70, + """ + info: HexInfo = hex_str_to_annotation(data, protocol_6=True, protocol_7=True) + print(info) + assert info['bytes'] == '3a 01 05 62 72 69 64 67 65 5f 70 69 63 6b 75 70' + +def test_comma_0xhex_multiline(): + # real 0.7 only packet payload + data = """ + 0x3a, 0x01, 0x05, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x5f, 0x70, 0x69, 0x63, 0x6b, 0x75, 0x70, 0x73, + 0x00, 0x91, 0xa7, 0xf2, 0xa0, 0x05, 0x8b, 0x11, 0x08, 0xa8, 0x15, 0xa9, 0x19, 0x38, 0xaf, 0xfd, 0xb6, + 0xcb, 0xf1, 0xdb, 0x5a, 0xfc, 0x73, 0x34, 0xae, 0xa3, 0x68, 0xa7, 0xfa, 0x35, 0x54, 0x37, 0xf9, 0x39, + 0x96, 0xd6, 0x05, 0x25, 0xef, 0x7b, 0x22, 0x40, 0x9d, + """ + info: HexInfo = hex_str_to_annotation(data, protocol_6=False, protocol_7=True) + print(info) + assert info['bytes'] == ( + '3a 01 05 62 72 69 64 67 65 5f 70 69 63 6b 75 70 73 00 91 a7 f2 a0 05 8b 11 08 ' + 'a8 15 a9 19 38 af fd b6 cb f1 db 5a fc 73 34 ae a3 68 a7 fa 35 54 37 f9 39 96 ' + 'd6 05 25 ef 7b 22 40 9d' + ) + def test_space_comma_0xhex(): info: HexInfo = hex_str_to_annotation('0x01, 0x02', protocol_6=True, protocol_7=True) print(info)