Converting Little Endian to Big Endian

Since a great part of writing software is about reusing existing solutions, the first thing should always be a look into the documentation for your language/library. reverse = Integer.reverseBytes(x); I don’t know how efficient this function is, but for toggling lots of numbers, a ByteBuffer should offer decent performance. import java.nio.ByteBuffer; import java.nio.ByteOrder; … int[] … Read more

Is there any “standard” htonl-like function for 64 bits integers in C++?

#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) #define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) The test (1==htonl(1)) simply determines (at runtime sadly) if the hardware architecture requires byte swapping. There aren’t any portable ways to determine at compile-time what … Read more

What belongs in an educational tool to demonstrate the unwarranted assumptions people make in C/C++?

The order of evaluation of subexpressions, including the arguments of a function call and operands of operators (e.g., +, -, =, * , /), with the exception of: the binary logical operators (&& and ||), the ternary conditional operator (?:), and the comma operator (,) is Unspecified For example int Hello() { return printf(“Hello”); /* … Read more

Is char *envp[] as a third argument to main() portable

The function getenv is the only one specified by the C standard. The function putenv, and the extern environ are POSIX-specific. EDIT The main parameter envp is not specified by POSIX but is widely supported. An alternative method of accessing the environment list is to declare a third argument to the main() function: int main(int … Read more