The system may not support htonll, ntohll.
Implement the functions by macro.
You need to check the byte order macro.
If you don't know this system's byte ordering, use is_big_endian() function below.
I tested this code.
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <inttypes.h>
#if __BIG_ENDIAN__
#define htonll(x) (x)
#define ntohll(x) (x)
#else
#define htonll(x) ((((uint64_t)htonl(x&0xFFFFFFFF)) << 32) + htonl(x >> 32))
#define ntohll(x) ((((uint64_t)ntohl(x&0xFFFFFFFF)) << 32) + ntohl(x >> 32))
#endif
void dump_bin(unsigned char *p, int len) {
int i=0;
for (i=0; i<len; i++) {
printf("%02x ", p[i]) ;
}
printf("\n");
}
int is_big_endian() {
union {
int i ;
char c[4] ;
} v = { 0x00000001 } ;
return v.c[3]==1 ;
}
int main() {
#if __BIG_ENDIAN__
printf("macro: big_endian.\n") ;
#else
printf("macro: little_endian.\n") ;
#endif
printf("System is big endian? : %d\n", is_big_endian());
long long ll=123456789012345678L;
long long ll2=0;
printf("long long value=%lld\n", ll) ;
dump_bin((unsigned char*)&ll, sizeof(ll)) ;
ll2=htonll(ll) ;
printf("htonll=%lld\n", ll2) ;
dump_bin((unsigned char*)&ll2, sizeof(ll2)) ;
return 0 ;
}
The output is here.
macro: little_endian.
System is big endian? : 0
long long value=123456789012345678
4e f3 30 a6 4b 9b b6 01
htonll=5688944245090268673
01 b6 9b 4b a6 30 f3 4e