Skip to content

Commit 70b2361

Browse files
authored
Arxml limits number formats (#457)
* add number decoding in arxml for different basis * fix #454
1 parent 270f409 commit 70b2361

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/canmatrix/formats/arxml.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ def decode_compu_method(compu_method, root_or_cache, ns, float_factory):
918918
# Modification to support sourcing the COMPU_METHOD info from the Vector NETWORK-REPRESENTATION-PROPS
919919
# keyword definition. 06Jun16
920920
#####################################################################################################
921-
if ll is not None and desc is not None and int(float_factory(ul.text)) == int(float_factory(ll.text)):
921+
if ll is not None and desc is not None and canmatrix.utils.decode_number(ul.text) == canmatrix.utils.decode_number(ll.text):
922922
#####################################################################################################
923923
#####################################################################################################
924924
values[ll.text] = desc
@@ -1187,7 +1187,7 @@ def get_signals(signal_array, frame, root_or_cache, ns, multiplex_id, float_fact
11871187
new_signal.initial_value = float_factory(initvalue.text)
11881188

11891189
for key, value in list(values.items()):
1190-
new_signal.add_values(key, value)
1190+
new_signal.add_values(canmatrix.utils.decode_number(key), value)
11911191
if signal_name is not None:
11921192
new_signal.add_attribute("LongName", signal_name)
11931193
frame.add_signal(new_signal)

src/canmatrix/tests/test_utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ def test_utils_guess_value():
1010
assert canmatrix.utils.guess_value("False") == "0"
1111
assert canmatrix.utils.guess_value("faLse") == "0"
1212

13+
def test_decode_number():
14+
assert canmatrix.utils.decode_number("0x10") == 16
15+
assert canmatrix.utils.decode_number("0b10") == 2
16+
assert canmatrix.utils.decode_number("10") == 10

src/canmatrix/utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,21 @@ def get_gcd(value1, value2): # type (int,int) -> (int)
6060
return math.gcd(value1, value2)
6161
else:
6262
return fractions.gcd(value1, value2)
63+
64+
def decode_number(value): # type(string) -> (int)
65+
"""
66+
Decode string to integer and guess correct base
67+
:param value: string input value
68+
:return: integer
69+
"""
70+
71+
value = value.strip()
72+
base = 10
73+
if len(value) > 1 and value[1] == 'b': # bin coded
74+
base = 2
75+
value = value[2:]
76+
if len(value) > 1 and value[1] == 'x': # hex coded
77+
base = 16
78+
value = value[2:]
79+
80+
return int(value, base)

0 commit comments

Comments
 (0)