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

          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)
          主站蜘蛛池模板: 靖州| 巴马| 南投市| 天长市| 洞头县| 东方市| 贞丰县| 贵阳市| 绥滨县| 湾仔区| 庆安县| 卢龙县| 高邑县| 乌拉特后旗| 隆化县| 湘西| 赫章县| 内黄县| 松溪县| 图们市| 双辽市| 日喀则市| 潮州市| 金华市| 婺源县| 绥中县| 天水市| 广饶县| 嘉峪关市| 广安市| 瓦房店市| 时尚| 玉环县| 泸溪县| 昌都县| 台中县| 五寨县| 鄂托克前旗| 永定县| 绍兴市| 东港市|