Add swap endian

master
Geoffrey Merck 4 years ago
parent 96d85c5f40
commit 1740db2c54

@ -425,4 +425,3 @@ void CUtils::truncateFile(const std::string& fileName)
if(file.is_open()) if(file.is_open())
file.close(); file.close();
} }

@ -17,6 +17,8 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <climits>
#include <memory>
enum TRISTATE { enum TRISTATE {
STATE_FALSE, STATE_FALSE,
@ -45,4 +47,23 @@ public:
static void ReplaceChar(std::string &str, char from, char to); static void ReplaceChar(std::string &str, char from, char to);
static time_t parseTime(const std::string str); static time_t parseTime(const std::string str);
static void truncateFile(const std::string& fileName); static void truncateFile(const std::string& fileName);
template <typename T>
static T swap_endian(T u)
{
static_assert (CHAR_BIT == 8, "CHAR_BIT != 8");
union
{
T u;
unsigned char u8[sizeof(T)];
} source, dest;
source.u = u;
for (size_t k = 0; k < sizeof(T); k++)
dest.u8[k] = source.u8[sizeof(T) - k - 1];
return dest.u;
}
}; };

Loading…
Cancel
Save

Powered by TurnKey Linux.