Skip to content

Commit 13ebc8b

Browse files
committed
Clang doesn't like ntohl (I misread it as being 64 bit); swapped to generic template
1 parent 4d23774 commit 13ebc8b

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

include/libdbc/utils/utils.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
129153
class String {
130154
public:
131155
static std::string trim(const std::string& line) { return Utils::trim(line); }

src/message.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include <arpa/inet.h>
2-
31
#include <cstddef>
42
#include <cstdint>
53
#include <libdbc/message.hpp>
@@ -8,6 +6,8 @@
86
#include <string>
97
#include <vector>
108

9+
#include <libdbc/utils/utils.hpp>
10+
1111
namespace Libdbc {
1212

1313
constexpr 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

0 commit comments

Comments
 (0)