Skip to content

Commit a9c2ef3

Browse files
committed
add number decoding in arxml for different basis
possible fix for #454
1 parent 270f409 commit a9c2ef3

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/canmatrix/formats/arxml.py

+1-1
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

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)