Posted on 2013-07-23 16:42 
Milo的海域 阅读(3331) 
评论(0)  编辑  收藏  所属分类: 
C 
			
			
		 
		#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <inttypes.h>
uint64_t htonll(uint64_t val) {
    return (((uint64_t) htonl(val)) << 32) + htonl(val >> 32);
}
uint64_t ntohll(uint64_t val) {
    return (((uint64_t) ntohl(val)) << 32) + ntohl(val >> 32);
}
int main() {
    uint64_t hll = 0x1122334455667788;
    printf("uint64: %"PRIu64"\n", hll);
    printf("0x%"PRIX64"\n", hll);
    printf("htonll(hll) = 0x%"PRIX64"\n", htonll(hll));
    printf("ntohll(htonll(hll)) = 0x%"PRIX64"\n", ntohll(htonll(hll)));
    printf("ntohll(hll) = 0x%"PRIX64"\n", ntohll(hll)); // no change
    return 1;
}
 big endian(network byte order), little endian (host byte order in intel arch)