How do I convert between big-endian and little-endian values in C++? I'm using VC++ 6.0.when I used _byteswap_ulong() function it requires the header file intrin.h. When I include the header it reports an error saying incompatible compiler and that intrin.h is for the gcc compiler. So is there any other functions to convert between big-endian and little-endian values in VC++ other than this function?
anything starting with an underscore is non-standard by definition.
Evan Teran
2009-03-15 18:18:45
I don't think he was asking for a standard way, he was asking for a way in VC++, unless I am wildly mistaken?
Logan Capaldo
2009-03-15 18:48:47
+8
A:
In POSIX-compliant systems, you have the standard byteswap(3) functions:
#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
These are intended specifically for network use, as suggested by the name (host-to-network-long, host-to-network-short, etc.)
GNU and BSD systems also provide the endian(3) functions:
#define _BSD_SOURCE
#include <endian.h>
uint16_t htobe16(uint16_t host_16bits);
uint16_t htole16(uint16_t host_16bits);
uint16_t be16toh(uint16_t big_endian_16bits);
uint16_t le16toh(uint16_t little_endian_16bits);
uint32_t htobe32(uint32_t host_32bits);
uint32_t htole32(uint32_t host_32bits);
uint32_t be32toh(uint32_t big_endian_32bits);
uint32_t le32toh(uint32_t little_endian_32bits);
uint64_t htobe64(uint64_t host_64bits);
uint64_t htole64(uint64_t host_64bits);
uint64_t be64toh(uint64_t big_endian_64bits);
uint64_t le64toh(uint64_t little_endian_64bits);
(On OpenBSD, the number for bit width is always at the end of the function: be64toh() vs. betoh64(), for example.)
Otherwise, most people define their own macros or functions:
#define bswap16(x) ((x)>>8 | ((x)&255)<<8)
#deinfe bswap32(x) ((bswap16((x)>>16)&65535)|(bswap16((x)&65535)<<16))
/* etc. */
You can also use assembly intrinsics, like with the bswap instruction on x86. __builtin_bswap64
in GCC and ICC. Something similar is in VS since VS7.0.
greyfade
2009-03-15 17:45:00
+1, but I don't think you need to use intrinsics because they are used automatically when you use the library functions.
Jason Cohen
2009-03-15 17:54:17
I don't rely on that. I realize some toolchains are smart enough to do it, but I still never see a compiler turn a fsqrt() call into the 3-instruction SSE equivalent.
greyfade
2009-03-15 17:59:39
greyfade: It depends. VC++ has an option to use intrinsics for built-in functions. And of course, you need to target a platform that supports SSE. GCC has similar options. But of course, if you don't specify to the compiler that SSE is available, it won't be used.
jalf
2009-03-15 18:19:42
Oh, I know this. But even GCC with an appropriate -march switch still sometimes doesn't optimize it that way. (Or, rather, at least not since before GCC 4. I haven't done much experimenting lately. I simply don't rely on it.)
greyfade
2009-03-15 18:51:27