posts - 88, comments - 3, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

htonll & ntohll in C

Posted on 2013-07-23 16:42 Milo的海域 阅读(3280) 评论(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)

只有注册用户登录后才能发表评论。


网站导航: