使用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);

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

  1. android raw rgb 转换器,raw图像格式转换工具——Iridient Developer Mac

    原标题:raw图像格式转换工具--Iridient Developer Mac Iridient Developer是一款功能强大的RAW图像转换应用程序,专为Mac OS X设计和优化.Iridie ...

  2. Java字节序与大小端转换_什么时候要进行大小端字节序的转换? | 学步园

    什么时候要进行大小端字节序的转换? 通过socket收发数据(在intel的处理器上), 什么时候要进行大小端字节序的转换? 比如发一个int型的数据出去,要不要进行转换? 还是两端一样,就不用考虑转 ...

  3. 什么时候要进行大小端字节序的转换?

    什么时候要进行大小端字节序的转换? 通过socket收发数据(在intel的处理器上), 什么时候要进行大小端字节序的转换? 比如发一个int型的数据出去,要不要进行转换? 还是两端一样,就不用考虑转 ...

  4. 嵌入式端音频开发(实战篇)之 4.2 把很多音频合成一个bin文件并统计每个音频文件的大小的方法(内含转换脚本)

    查看本系列全部文章请点击:嵌入式端音频开发系列汇总(持续更新) 查看本文全部内容:嵌入式端音频开发(实战篇)之 4.2 把很多音频合成一个bin文件并统计每个音频文件的大小的方法(内含转换脚本) 文件 ...

  5. 大小端、字节顺序转换函数和IP地址格式转换函数

    大小端.字节顺序转换函数和IP地址格式转换函数 大小端 CPU大小端之分 常见字节序 字节顺序转换函数 Linux系统下定义 Windows系统下 ntohs() htons() htonl()和nt ...

  6. Android端与PC端同步绘画板(二)-适配手机和PC屏幕大小

    前言 关于手机端与PC端如何建立通讯,请参考: Android端与PC端同步绘画板(一)-使用Socket建立连接 这篇文章我们来简单说一下关于手机端和PC端的屏幕适配问题,因为我们都知道因为Andr ...

  7. android qq字体大小设置快捷键,电脑端QQ怎么将字体选择设置为气泡模式

    电脑端QQ怎么将字体选择设置为气泡模式 用惯了手机QQ的气泡模式,那么不妨把电脑也修改一下吧.今天小编就告诉大家电脑端QQ怎么将字体选择设置为气泡模式. 具体如下: 1. 打首先我们打开电脑中的QQ, ...

  8. 谷歌浏览器移动端部分字体大小与设定大小不同

    问题描述:为了适配各种移动端设备,动态设置了viewport缩放,然后动态设置html的font-size大小,用rem布局.但是在谷歌浏览器中查看的时候出现了问题 1.这个没问题,font-size ...

  9. android 仿微信demo————微信通讯录界面功能实现(移动端,服务端)

    android 仿微信demo----微信启动界面实现 android 仿微信demo----注册功能实现(移动端) android 仿微信demo----注册功能实现(服务端) android 仿微 ...

最新文章

  1. 大道至简阅读笔记03
  2. Mybatis优缺点
  3. Apriori算法介绍(Python实现)
  4. 65.shell特殊符号与和cut,sort,wc,uniq,tee,tr,split命令
  5. 域用户的登录过程和GC的关系
  6. 石板切割问题c语言_广场地面铺装 仿古石 青石板 板岩 切割岩 冰裂纹压印混凝土地坪...
  7. 面向多星多任务的大数据处理系统设计
  8. bootstrap-table使用 带条件查询翻页及数据更新的问题。
  9. 苹果计划在明年推出的高端iPhone机型上应用挖孔屏
  10. EL表达式+JSTL,forEach的两种用法
  11. 神策分析中的数据模型
  12. html超酷图片墙特效代码,超酷超绚精美图片展示效果代码(一)
  13. 如何使用Tenderly在Moonbeam上构建更优秀的Dapp
  14. 【附源码】计算机毕业设计SSM校园论坛设计
  15. GPU加速原理浅析及代码实现
  16. 有人负责,才有质量,写给在市集中迷失的一代读后感
  17. 网络管理软件都有哪些,目前好用的网络管理软件
  18. office word 2010 设置标题,样式,编号
  19. 为何家会伤人:2020-10-15早上
  20. C语言实现的一个交互小程序(一)

热门文章

  1. C++成员变量初始化列表中初始化顺序
  2. Windows上Chrome被“毒霸网页“劫持启动页解决方式
  3. Mac搭建本地服务器及测试demo
  4. android5.1不生成odex
  5. linux --- inotify 文件系统变化通知机制
  6. Android iw 使用方法
  7. Android系统信息获取 之十二:获取屏幕分辨率及密度
  8. c++笔试题整理(二)
  9. wpf之DragDrop研究
  10. webgis之相关工具