posts - 88, comments - 3, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          htonll & ntohll in C

          Posted on 2013-07-23 16:42 Milo的海域 閱讀(3314) 評論(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)
          主站蜘蛛池模板: 葵青区| 北辰区| 五寨县| 榕江县| 临猗县| 浦县| 茂名市| 从化市| 赞皇县| 谷城县| 西宁市| 麻江县| 都江堰市| 墨玉县| 广东省| 崇文区| 台湾省| 阿克陶县| 平乡县| 宝鸡市| 县级市| 谷城县| 上饶县| 绥滨县| 乐平市| 海阳市| 西和县| 无棣县| 裕民县| 七台河市| 滁州市| 华蓥市| 荃湾区| 滨海县| 恩平市| 蓬安县| 湾仔区| 阿拉尔市| 手游| 斗六市| 罗甸县|