2024-12-03 10:36:06 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/**
|
|
|
|
Copyright (c) 2015 - 2022 Beckhoff Automation GmbH & Co. KG
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2024-12-09 09:41:04 +08:00
|
|
|
namespace Beckhoff
|
2024-12-03 10:36:06 +08:00
|
|
|
{
|
|
|
|
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
|
|
|
|
template<class T>
|
|
|
|
inline T letoh(const void* buffer)
|
|
|
|
{
|
|
|
|
const auto bytes = reinterpret_cast<const uint8_t*>(buffer);
|
|
|
|
T result = 0;
|
|
|
|
for (size_t i = 0; i < sizeof(T); ++i) {
|
|
|
|
result += (bytes[i] << (8 * i));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
template<class T>
|
|
|
|
inline T letoh(const void* buffer)
|
|
|
|
{
|
|
|
|
return *reinterpret_cast<const T*>(buffer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline T letoh(const T& value)
|
|
|
|
{
|
|
|
|
return letoh<T>(reinterpret_cast<const uint8_t*>(&value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T htole(const T value)
|
|
|
|
{
|
|
|
|
return letoh(value);
|
|
|
|
}
|
2024-12-09 09:41:04 +08:00
|
|
|
}
|