Skip to content

Commit 1e7c7a7

Browse files
committed
Implement 2 complement to decimal
1 parent 6908fb9 commit 1e7c7a7

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/message.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,17 @@ Message::ParseSignalsStatus Message::parseSignals(const std::vector<uint8_t>& da
5454
case 64:
5555
values.push_back((int64_t)v * signal.factor + signal.offset);
5656
break;
57-
default:
58-
return ParseSignalsStatus::ErrorInvalidConversion;
57+
default: {
58+
// 2 complement -> decimal
59+
const int negative = (v & (1 << (signal.size - 1))) != 0;
60+
int64_t nativeInt;
61+
if (negative)
62+
nativeInt = v | ~((1 << signal.size) - 1);
63+
else
64+
nativeInt = v;
65+
values.push_back(nativeInt * signal.factor + signal.offset);
66+
break;
67+
}
5968
}
6069
} else
6170
values.push_back(v * signal.factor + signal.offset);

0 commit comments

Comments
 (0)