3 ms·
One fallacy is that you need to ever manually convert byte order yourself in the way the article suggests. Most systems have something that'll do it for you - e
by sparkie 15y ago
One fallacy is that you need to ever manually convert byte order yourself in the way the article suggests. Most systems have something that'll do it for you - eg, htonl, ntohl.
- ge0rg 15y agoUnfortunately, that only works for network-byte-ordered data. If you have LE data, you still have to do it yourself.
- huhtenberg 15y agoKeep in mind that on Windows htonl is a function call, and not an inline or a macro.
- dfox 15y agoMain point is that you should have function that converts array of bytes to integer or vice-versa, not something like htonl which either swaps order of bytes in integer or does nothing, depending on endianity of platform.
- ArchD 15y agoI don't know why you got downvoted. Your idea is valid and people may not have realized that htonl has friends an relatives that totally make the issue of the article moot. #define _BSD_SOURCE /* See feature_test_macros(7) */ #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);
- alexchamberlain 15y agoWhere are these defined?
- gkelly 15y agoI found them, on ubuntu, in: /usr/include/endian.h