File tree Expand file tree Collapse file tree 2 files changed +27
-3
lines changed
Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -126,6 +126,30 @@ constexpr bool isWhitespaceOrEmpty(const T& str) {
126126 return std::all_of (str.begin (), str.end (), [](char c) { return std::isspace (c); });
127127}
128128
129+ /* *
130+ * @brief Swaps the endianness of a multi-byte value.
131+ *
132+ * @tparam T The type of the value.
133+ * @param value The value to swap.
134+ * @return T The value with swapped endianness.
135+ *
136+ * @copyright 2025 Procyon Systems Inh. Simon Cahill (s.cahill@procyon-systems.de)
137+ */
138+ template <typename T>
139+ T swapEndianness (T value) {
140+ T swappedBytes;
141+
142+ char * valuePtr = reinterpret_cast <char *>(&value);
143+ char * swappedPtr = reinterpret_cast <char *>(&swappedBytes);
144+
145+ auto sizeInBytes = sizeof (T);
146+ for (size_t i = 0 ; i < sizeInBytes; i++) {
147+ swappedPtr[sizeInBytes - 1 - i] = valuePtr[i];
148+ }
149+
150+ return swappedBytes;
151+ }
152+
129153class String {
130154public:
131155 static std::string trim (const std::string& line) { return Utils::trim (line); }
Original file line number Diff line number Diff line change 1- #include < arpa/inet.h>
2-
31#include < cstddef>
42#include < cstdint>
53#include < libdbc/message.hpp>
86#include < string>
97#include < vector>
108
9+ #include < libdbc/utils/utils.hpp>
10+
1111namespace Libdbc {
1212
1313constexpr unsigned ONE_BYTE = 8 ;
@@ -39,7 +39,7 @@ Message::ParseSignalsStatus Message::parse_signals(const std::vector<uint8_t>& d
3939 for (std::size_t i = 0 ; i < size; i++) {
4040 data_big_endian = (data_big_endian << ONE_BYTE) | (uint64_t )data[i];
4141 }
42- data_little_endian = ntohl (data_little_endian);
42+ data_little_endian = Utils::swapEndianness (data_little_endian);
4343
4444 // TODO: does this also work on a big endian machine?
4545
You can’t perform that action at this time.
0 commit comments