网络字节序和主机字节序转换函数

2023-05-16

 大小端判断

#include <stdio.h>
#include <stdint.h>
int is_little_endian()
{
    union {
        uint32_t num;
        uint8_t c;
    }e;
    e.num = 1;
    return e.c;
}
int main(void)
{
    if(!is_little_endian()){
        printf("big endian\n");
    }else{
        printf("little endian\n");
    }
    return 0;
}
gcc endian.c -o endian

 由执行结果可知,是小端。

$ ./endian
little endian

一 大小端互转相关函数

AME
       htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh - convert values between host and big-/little-endian byte order

SYNOPSIS
       #define _BSD_SOURCE             /* See feature_test_macros(7) */
       #include <endian.h>

       uint16_t htobe16(uint16_t host_16bits);
       uint16_t htole16(uint16_t host_16bits);
       uint16_t be16toh(uint16_t big_endian_16bits);
       uint16_t le16toh(uint16_t little_endian_16bits);

       uint32_t htobe32(uint32_t host_32bits);
       uint32_t htole32(uint32_t host_32bits);
       uint32_t be32toh(uint32_t big_endian_32bits);
       uint32_t le32toh(uint32_t little_endian_32bits);

       uint64_t htobe64(uint64_t host_64bits);
       uint64_t htole64(uint64_t host_64bits);
       uint64_t be64toh(uint64_t big_endian_64bits);
       uint64_t le64toh(uint64_t little_endian_64bits);

DESCRIPTION
       These functions convert the byte encoding of integer values from the byte order that the current CPU (the "host") uses, to and from little-endian and big-endian byte order.

       The number, nn, in the name of each function indicates the size of integer handled by the function, either 16, 32, or 64 bits.

       The functions with names of the form "htobenn" convert from host byte order to big-endian order.

       The functions with names of the form "htolenn" convert from host byte order to little-endian order.

       The functions with names of the form "benntoh" convert from big-endian order to host byte order.

       The functions with names of the form "lenntoh" convert from little-endian order to host byte order.

二 转换函数 

进行网络字节序转换的函数有htons()、ntohs()、htonl()、ntohl()等,其中s是short数据类型的意思,l是long数据类型的意思,而h是host,即主机的意思,n是network,即网络的意思。以上4个函数分别如下。

htons():表示对于short类型的变量,从主机字节序转换为网络字节序。

ntohs():表示对于short类型的变量,从网络字节序转换为主机字节序。

htonl():表示对于long类型的变量,从主机字节序转换为网络字节序。

ntohl():表示对于long类型的变量,从网络字节序转换为主机字节序。

NAME
       htonl, htons, ntohl, ntohs - convert values between host and network byte order

SYNOPSIS
       #include <arpa/inet.h>

       uint32_t htonl(uint32_t hostlong);

       uint16_t htons(uint16_t hostshort);

       uint32_t ntohl(uint32_t netlong);

       uint16_t ntohs(uint16_t netshort);

DESCRIPTION
       The htonl() function converts the unsigned integer hostlong from host byte order to network byte order.

       The htons() function converts the unsigned short integer hostshort from host byte order to network byte order.

       The ntohl() function converts the unsigned integer netlong from network byte order to host byte order.

       The ntohs() function converts the unsigned short integer netshort from network byte order to host byte order.

       On the i386 the host byte order is Least Significant Byte first, whereas the network byte order, as used on the Internet, is Most Significant Byte first.

三 内核中转换相关宏最终实现

#define ___constant_swab16(x) ((__u16)(				\
	(((__u16)(x) & (__u16)0x00ffU) << 8) |			\
	(((__u16)(x) & (__u16)0xff00U) >> 8)))

#define ___constant_swab32(x) ((__u32)(				\
	(((__u32)(x) & (__u32)0x000000ffUL) << 24) |		\
	(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) |		\
	(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) |		\
	(((__u32)(x) & (__u32)0xff000000UL) >> 24)))

#define ___constant_swab64(x) ((__u64)(				\
	(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) |	\
	(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) |	\
	(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) |	\
	(((__u64)(x) & (__u64)0x00000000ff000000ULL) <<  8) |	\
	(((__u64)(x) & (__u64)0x000000ff00000000ULL) >>  8) |	\
	(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) |	\
	(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) |	\
	(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56)))

#define ___constant_swahw32(x) ((__u32)(			\
	(((__u32)(x) & (__u32)0x0000ffffUL) << 16) |		\
	(((__u32)(x) & (__u32)0xffff0000UL) >> 16)))

#define ___constant_swahb32(x) ((__u32)(			\
	(((__u32)(x) & (__u32)0x00ff00ffUL) << 8) |		\
	(((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))

小结

意htons()和ntohs()函数及htonl()和ntohl()函数是对应的转换,两个函数完全可以使用同一套代码,例如:

#define ntohl htonl

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

网络字节序和主机字节序转换函数 的相关文章

  • 集成学习

    李宏毅机器学习 周志华机器学习第8章 使用sklearn进行集成学习 stacking心得 xgboost实战 bagging xff1a 当原模型已经很复杂的时候 xff0c bias已经很小但variance很大时候 比较容易overf
  • 【C++中级篇】gtest的使用方法及cmake配置

    个人箴言 xff1a 不积跬步 xff0c 无以至千里 xff1b 不积小流 xff0c 无以成江海 夯实基础 xff0c 成就高楼大厦 前言 xff1a 在一项c 43 43 工程中 xff0c 会存在很多类 xff0c 很多功能方法 x
  • CMake如何控制两个文件生成的依赖关系

    使用target link libraries函数 xff1a 这个函数可以让你指定一个目标 xff08 target xff09 依赖于哪些其他目标或库 CMake会自动根据这些依赖关系来确定编译的顺序 xff0c 先编译被依赖的目标 x
  • 关于windows连不上服务器的问题

    报错 xff1a kex exchange identification read Connection reset 也有可能报错 xff1a remote side unexpectedly closed network connecti
  • Jackson多态反序列化的使用

    缘起 最近看Apache Druid的源代码 0 5很老的版本 xff0c 印象最深的就是对Jackson的多态反序列化和注入的使用了 xff0c 这里也属于自己的知识盲点 xff0c 看着复杂的json直接反序列化为可用对象 xff0c
  • Apache Druid源码导读--Google guice DI框架

    文章目录 缘起Google Guice介绍与Spring的对比Example覆盖已有绑定关系默认绑定 Apache Druid中Guice模块guice lifecycleguice jsonconfigguice jersey jetty
  • 向Python女神推荐这些年我追过的经典书籍

    最近 34 瑞丽模特学Python 34 的热点牵动了大江南北程序员的心 xff0c 有人说这是炒作 xff0c 也有人说这是推广Python的爆点 我嘿嘿一笑 xff0c 美女就是美女 xff0c 眼光那是杠杠的 xff0c 不仅人美 x
  • [gevent源码分析] 深度分析gevent运行流程

    一直对gevent运行流程比较模糊 xff0c 最近看源码略有所得 xff0c 不敢独享 xff0c 故分享之 gevent是一个高性能网络库 xff0c 底层是libevent xff0c 1 0版本之后是libev xff0c 核心是g
  • TCP服务器端和客户端程序设计

    一 实验目的 学习和掌握Linux下的TCP服务器基本原理和基本编程方法 体会TCP与UDP编程的不同 xff0c UDP编程 xff1a http blog csdn net yueguanghaidao article details
  • UDP服务器端和客户端程序设计

    实验三 UDP服务器端程序设计 一 实验目的 学习和掌握Linux下的UDP服务器基本原理和基本编程方法 xff0c 体会与TCP的区别 xff0c TCP编程 xff1a http blog csdn net yueguanghaidao
  • python实现的文本编辑器

    wxpython实现的文本编辑器 效果如下 xff1a 主要功能 xff1a 1 编辑保存文本 xff0c 打开修改文本 2 常用快捷键 xff0c 复制 xff0c 粘贴 xff0c 全选等 3 支持撤销功能 4 支持弹出式菜单 代码如下
  • C语言开发Linux下web服务器(支持GET/POST,SSL,目录显示等)

    这个主要是在CSAPP基础上做的 xff0c 添加了POST xff0c SSL xff0c 目录显示等功能 一 实现功能 xff1a 1 支持GET POST方法 2 支持SSL安全连接即HTTPS 3 支持CGI 4 基于IP地址和掩码
  • sklearn2pmml xgboost缺失值(missing)处理的坑

    sklearn2pmml xgboost缺失值 missing 处理的坑 今天同事在部署xgboost pmml模型时遇到了大坑 xff0c 线上spark预测和本地python预测结果怎么都不对应 xff0c 记录一下处理过程 看了下同事
  • adb导出手机应用到电脑

    简单说一下相关步骤 xff0c 以备不时之需 1 手机开启usb调试 2 Windows系统 Win 43 R打开命令行窗口 xff0c 输入adb devices xff0c 如果连接成功会出现机子的序列号 3 adb shell pm
  • Js作用域与作用域链详解

    一直对Js的作用域有点迷糊 xff0c 今天偶然读到Javascript权威指南 xff0c 立马被吸引住了 xff0c 写的真不错 我看的是第六版本 xff0c 相当的厚 xff0c 大概1000多页 xff0c Js博大精深 xff0c
  • windows10环境下tensorflow安装教程

    楼主最近一直忙着找工作 最近几个月一直all in java 好久没学机器学习 深度学习 前几天突然通知要提交论文中期了 于是赶紧打开电脑 结果发现之前安装的tensorflow居然登陆不上了 折腾了半天 搜过各种csdn博客 一直安装失败
  • 'gbk' codec can't encode character '\xa0'

    从网上抓了一些字节流 xff0c 想打印出来结果发生了一下错误 xff1a UnicodeEncodeError 39 gbk 39 codec can 39 t encode character 39 xbb 39 in position
  • 【Git记录学习】github创建项目以及本地使用(vscode)

    一 github创建空仓库 从github中创建空仓库 在执行完上一步操作后会返回这样的界面 xff0c 包括了一些基本的git操作以及HttpS SSH地址 生成一个readme md文档 xff08 步骤2 Set up下面有蓝色的超链
  • 关于DFT变换含义、公式和具体形式

    原文地址 xff1a http blog sina com cn s blog 7853c3910102v9wd html 这篇文章从实际工程应用的角度 xff0c 记录一下如何计算 xff0c 关于公式 变形和应用 维基百科上的 DFT公
  • 1602显示数字不稳定一直跳动(AD转换)

    程序如下所示 首先说明下 xff0c 此程序为AD转换芯片PCF8591采集电压数据 xff0c 然后送到1602显示 现象 xff1a 1602显示的数字一直频繁的跳动 xff0c 乱花眼 此现象不是一直出现的 xff0c 有时候会出现

随机推荐

  • C++11中的线程类

    前面介绍的线程是利用了POSIX线程库 xff0c 这是传统C C 43 43 程序员使用线程的方式 xff0c 而C 43 43 11提供了语言层面使用线程的方式 C 43 43 11新标准中引入了5个头文件来支持多线程编程 xff0c
  • 4.4.1内核编译

    内核源码下载地址 xff1a https mirrors edge kernel org pub linux kernel v4 x linux 4 4 1 tar gz 安装依赖包 xff1a 报错就装 cp boot config xx
  • fatal error: hugetlbfs.h: No such file or directory

    fatal error hugetlbfs h No such file or directory 解决办法 xff1a sudo apt get update sudo apt get install libhugetlbfs dev
  • WSL下 配置NFS-失败

    配置一个IP地址 xff1a sudo ip addr add 192 168 250 2 24 broadcast 192 168 250 255 dev eth2 sudo apt get install nfs kernel serv
  • OMT 对象模型、动态模型和功能模型

    对象模型描述系统中对象的静态结构 对象之间的关系 对象的属性 对象的操作 对象模型表示静态的 结构上的 系统的 数据 34 特征 对象模型为动态模型和功能模型提供了基本的框架 xff0c 对象模型用包含对象和类的对象图来表示 OMT的对象模
  • 关于epoll的调试的几个问题

    将今天调试的几个小问题点总结下 xff0c 后续遇到再添加 一 将总结的问题点放在最前面 1 epoll wait的maxevents参数 epoll wait的maxevents参数 xff0c 经过测试 xff0c maxevents的
  • poll函数测试

    一 基础知识 include lt poll h gt int poll struct pollfd fds nfds t nfds int timeout 其中参数fds指向一个结构体数组的第0个元素的指针 xff0c 每个数组元素都是一
  • IPC:匿名管道和命名管道

    一 管道初级测试 写两个小程序 xff0c 一个负责向管道发数据 xff0c 一个从管道接收数据 xff1b pipe cpp include lt iostream gt using namespace std int main cout
  • IPC:system V消息队列

    ftok函数 ftok convert a pathname and a project identifier to a System V IPC key SYNOPSIS include lt sys types h gt include
  • IPC:system V 信号量和共享内存

    信号量相关知识 结构体 union semun int val Value for SETVAL struct semid ds buf Buffer for IPC STAT IPC SET unsigned short array Ar
  • 信号signal编程测试

    信号会打断系统调用 xff0c 慎用 xff0c 就是用的时候测一测 下面是信号的基础测试 信号 信号 xff08 signal xff09 机制是UNIX系统中最为古老的进程之间的通信机制 它用于在一个或多个进程之间传递异步信号 信号可以
  • 线程间通讯的信号量semaphore.h

    sem init SEM INIT 3 Linux Programmer 39 s Manual SEM INIT 3 NAME sem init initialize an unnamed semaphore SYNOPSIS inclu
  • posix线程的优先级测试

    测试的时候 xff0c 如果创建的线程不够多 xff0c 有些问题体现不出来 xff0c 例如pthread cond signal和pthread cond broadcast 奇怪的优化是不会有好结果的 优先级打印 xff1a 测试目的
  • getpwent系统调用

    getpwent系统调用 NAME getpwent setpwent endpwent get password file entry SYNOPSIS include lt sys types h gt include lt pwd h
  • 调试信息:linux彩色调试信息的输出

    一 printf实现的宏 测试代码 xff1a include lt sys types h gt include lt pwd h gt include lt stdio h gt include lt stdlib h gt defin
  • UML里面静态建模及动态建模都有哪些图?

    静态建模 xff1a 创建并建立一个系统的静态特征 1 用例图 xff1a 描述系统功能及功能的使用者 2 类 图 xff1a 表现系统里实体的关系 责任 类和类之间的关系 xff0c 属性及方法 3 对象图 xff1a 当类图不能完全显示
  • warning: ISO C++11 requires at least one argument for the “...“ in a variadic macro

    关于警告 xff1a warning ISO C 43 43 11 requires at least one argument for the 34 34 in a variadic macro 相关代码如下 xff0c 把下面的代码放到
  • IMX6开发板设置DHCP功能和验证DNS功能

    一 udhcpc命令 root 64 host udhcpc help BusyBox v1 29 3 2022 11 09 15 51 05 CST multi call binary Usage udhcpc fbqRB a MSEC
  • 常用端口号/etc/services

    etc services文件存储的内容 记录一下 xff0c 防止忘记 etc services Id services v 1 1 2004 10 09 02 49 18 andersen Exp Network services Int
  • 网络字节序和主机字节序转换函数

    大小端判断 include lt stdio h gt include lt stdint h gt int is little endian union uint32 t num uint8 t c e e num 61 1 return