From 386d58b5862d8b76925c6523721594887606e82a Mon Sep 17 00:00:00 2001 From: faketruth Date: Mon, 3 Oct 2011 18:41:19 +0000 Subject: MCServer c++ source files git-svn-id: http://mc-server.googlecode.com/svn/trunk@3 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/Endianness.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 source/Endianness.h (limited to 'source/Endianness.h') diff --git a/source/Endianness.h b/source/Endianness.h new file mode 100644 index 000000000..9e62c48c4 --- /dev/null +++ b/source/Endianness.h @@ -0,0 +1,51 @@ +#pragma once + +#include +#ifdef _WIN32 +#include +#else +#include +#include +#endif + +// Changes endianness +inline unsigned long long HostToNetwork8( void* a_Value ) +{ + unsigned long long __HostToNetwork8; + memcpy( &__HostToNetwork8, a_Value, sizeof( __HostToNetwork8 ) ); + __HostToNetwork8 = (( ( (unsigned long long)htonl((u_long)__HostToNetwork8) ) << 32) + htonl(__HostToNetwork8 >> 32)); + return __HostToNetwork8; +} + +inline unsigned int HostToNetwork4( void* a_Value ) +{ + unsigned int __HostToNetwork4; + memcpy( &__HostToNetwork4, a_Value, sizeof( __HostToNetwork4 ) ); + __HostToNetwork4 = ntohl( __HostToNetwork4 ); + return __HostToNetwork4; +} + +inline double NetworkToHostDouble8( void* a_Value ) +{ +#define ntohll(x) ((((unsigned long long)ntohl((u_long)x)) << 32) + ntohl(x >> 32)) + unsigned long long buf = 0;//(*(unsigned long long*)a_Value); + memcpy( &buf, a_Value, 8 ); + buf = ntohll(buf); + double x; + memcpy(&x, &buf, sizeof(double)); + return x; +} + +inline long long NetworkToHostLong8( void* a_Value ) +{ + unsigned long long buf = *(unsigned long long*)a_Value; + buf = ntohll(buf); + return *reinterpret_cast(&buf); +} + +inline float NetworkToHostFloat4( void* a_Value ) +{ + u_long buf = *(u_long*)a_Value; + buf = ntohl( buf ); + return *(float*)reinterpret_cast(&buf); +} \ No newline at end of file -- cgit v1.2.3