android 大小端转换器,大小端转换的C++ 封装 :hton , ntol

使用C++的模板、自动类型推导技术

#ifndef HtoN_h

#define HtoN_h

#include

#if __ANDROID__

#ifndef htonll

#define htonll(x) htonq(x)

#endif

#ifndef ntohll

#define ntohll(x) ntohq(x)

#endif

#endif

namespace zxszl

{

template T zl_hton (T t) { return t;}

template <> inline uint16_t zl_hton (uint16_t t) { return htons(t); }

template <> inline int16_t zl_hton (int16_t t) { return htons(t); }

template <> inline uint32_t zl_hton (uint32_t t) { return htonl(t); }

template <> inline int32_t zl_hton (int32_t t) { return htonl(t); }

template <> inline uint64_t zl_hton (uint64_t t) { return htonll(t); }

template <> inline int64_t zl_hton (int64_t t) { return htonll(t); }

template T zl_ntoh (T t) { return t;}

template <> inline uint16_t zl_ntoh (uint16_t t) { return ntohs(t); }

template <> inline int16_t zl_ntoh (int16_t t) { return ntohs(t); }

template <> inline uint32_t zl_ntoh (uint32_t t) { return ntohl(t); }

template <> inline int32_t zl_ntoh (int32_t t) { return ntohl(t); }

template <> inline uint64_t zl_ntoh (uint64_t t) { return ntohll(t); }

template <> inline int64_t zl_ntoh (int64_t t) { return ntohll(t); }

class __judge_little_endian

{

public:

static inline bool isle() { static const int _a = 0xAABBCCDD; return *(unsigned char *)(&_a) == 0xDD;}

};

#define zl_rorder_s(x) \

((__uint16_t)((((__uint16_t)(x) & 0xff00) >> 8) | \

(((__uint16_t)(x) & 0x00ff) << 8)))

#define zl_rorder_l(x) \

((__uint32_t)((((__uint32_t)(x) & 0xff000000) >> 24) | \

(((__uint32_t)(x) & 0x00ff0000) >> 8) | \

(((__uint32_t)(x) & 0x0000ff00) << 8) | \

(((__uint32_t)(x) & 0x000000ff) << 24)))

#define zl_rorder_ll(x) \

((__uint64_t)((((__uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \

(((__uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \

(((__uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \

(((__uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \

(((__uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \

(((__uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \

(((__uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \

(((__uint64_t)(x) & 0x00000000000000ffULL) << 56)))

template T zl_htole (T t) { return t;}

template <> inline uint16_t zl_htole (uint16_t t) {return __judge_little_endian::isle() ? t : zl_rorder_s(t);}

template <> inline int16_t zl_htole (int16_t t) {return __judge_little_endian::isle() ? t : zl_rorder_s(t);}

template <> inline uint32_t zl_htole (uint32_t t) {return __judge_little_endian::isle() ? t : zl_rorder_l(t);}

template <> inline int32_t zl_htole (int32_t t) {return __judge_little_endian::isle() ? t : zl_rorder_l(t);}

template <> inline uint64_t zl_htole (uint64_t t) { return __judge_little_endian::isle() ? t : zl_rorder_ll(t); }

template <> inline int64_t zl_htole (int64_t t) { return __judge_little_endian::isle() ? t : zl_rorder_ll(t); }

template T zl_letoh (T t) { return t;}

template <> inline uint16_t zl_letoh (uint16_t t) {return __judge_little_endian::isle() ? t : zl_rorder_s(t);}

template <> inline int16_t zl_letoh (int16_t t) {return __judge_little_endian::isle() ? t : zl_rorder_s(t);}

template <> inline uint32_t zl_letoh (uint32_t t) {return __judge_little_endian::isle() ? t : zl_rorder_l(t);}

template <> inline int32_t zl_letoh (int32_t t) {return __judge_little_endian::isle() ? t : zl_rorder_l(t);}

template <> inline uint64_t zl_letoh (uint64_t t) { return __judge_little_endian::isle() ? t : zl_rorder_ll(t); }

template <> inline int64_t zl_letoh (int64_t t) { return __judge_little_endian::isle() ? t : zl_rorder_ll(t); }

#undef zl_rorder_s

#undef zl_rorder_l

#undef zl_rorder_ll

}

#endif /* HtoN_h */

使用示例

using namespace zxszl;

short m = 10;

short n = zl_hton(m);

m = zl_ntoh(n);

int x = 100;

int y = zl_hton(x);

x = zl_ntoh(y);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值