Converting endianess portably

Is there an Unreal engine version of htons() and related endian conversion functions? I’m writing socket code that converts from network order to little endian order and I can’t find any documentation suggesting that theres anything for this. I’m using these includes currently for the functions but they mess up some macros and I doubt that it supports all of the platforms UE ships to

#ifdef _WIN32
#include <Winsock2.h>
#elif __unix__
#include <arpa/inet.h>
#endif

I’m wondering the same thing. All I found was FGenericPlatformProperties::IsLittleEndian. Then I swap them manually… I don’t want to risk hitting a platform without htons.

Did you figure it out?

Hi, You can use the function and macros in “Misc/ByteOrder.h”

#include "Misc/ByteSwap.h"

uint16 BYTESWAP_ORDER16(uint16 Val);
int16 BYTESWAP_ORDER16(int16 Val);
uint32 BYTESWAP_ORDER32(uint32 Val);
int32 BYTESWAP_ORDER32(int32 val);

The macros:

#define NETWORK_ORDER16(x) ...
#define NETWORK_ORDER32(x) ...
#define NETWORK_ORDER64(x) ...

And generic functions:

template<typename T> T ByteSwap(T Value);
1 Like