UNIX网络编程卷1(第三版)字节排序函数测试
来源:互联网 发布:河南青峰网络 编辑:程序博客网 时间:2024/06/05 06:50
内存中存储多字节有两种方法,即小端字节序和大端字节序,Ubuntu10.04是小端字节序,网际协议所用的字节序为大端字节序
内存地址增长方向: 低序字节->高序字节 小端字节序
高序字节->低序字节 大端字节序
主机和网络字节序转换函数在头文件<netinet/in.h>中,函数分别为:
uint16_t htons(uint16_t)
uint32_t htonl(uint32_t)
uint16_t ntohs(uint16_t)
uint32_t ntohl(uint32_t)
命名规则为: h-host,n-network,s-short,l-long, to 即变换到
测试程序为 byteorder_convert_fun1.cpp:
#include <iostream>#include <iomanip>#include <netinet/in.h>using std::cin;using std::cout;using std::endl;using std::hex;typedefunsigned short int uint16_t;typedef unsigned int uint32_t;int main(int argc, char **argv){uint16_t c1,d1;uint32_t c2,d2;c1 = 0x1A2B;c2 = 0x1A2B3C4D;cout<<hex<<c1<<'\t'<<"htons(c1) = "<<htons(c1)<<endl;cout<<hex<<c2<<'\t'<<"htonl(c2) = "<<htonl(c2)<<endl;d1 = 0x3E4F;d2 = 0x3E4F5A6B; cout<<hex<<d1<<'\t'<<"ntohs(d1) = "<<ntohs(d1)<<endl; cout<<hex<<c2<<'\t'<<"ntohl(d2) = "<<ntohl(d2)<<endl;return 0;}
root@gujinjin-desktop:/home/gujinjin/Socket# ./byteorder_convert_fun1 1a2b htons(c1) = 2b1a1a2b3c4d htonl(c2) = 4d3c2b1a3e4f ntohs(d1) = 4f3e3e4f5a6b ntohl(d2) = 6b5a4f3e
说明:
程序中用typedef 重新定义了 uint16_t 和 uint32_t, 也可以不用定义直接包含头文件 <sys/types.h>
0 0
- UNIX网络编程卷1(第三版)字节排序函数测试
- UNIX网络编程卷1(第三版)关于源程序编译出错问题的详细解决方法
- UNIX网络编程(第三版)测试环境搭建
- Unix 网络编程卷1
- unix网络编程卷一: 字节操纵函数(bzero和memset,bcopy和memcpy,bcmp和memcpy)
- UNIX网络编程 卷1 第三版 人邮版 环境搭建---unp.h
- Unix网络编程第一卷(第三版) 第一个程序运行成功
- 《UNIX网络编程 卷1》 笔记: readn和writen函数
- 《UNIX网络编程 卷1》 笔记: 高级I/O函数
- Unix网络编程(二):字节排序与地址转换函数
- 《UNIX网络编程 卷2》 笔记: 字节流与消息
- UNIX网络编程卷一 第三章 地址定义及相关函数
- 《UNIX网络编程 卷1》 笔记: readline函数 (不可重入版本)
- 《UNIX网络编程 卷1》 笔记: readline函数 (可重入版本)
- 《UNIX网络编程卷1》读书笔记--第三章套接字编程简介
- 读书笔记之《UNIX网络编程 第一卷》第三章
- UNIX网络编程:卷1-读书笔记
- Unix网络编程卷1源代码使用
- 帧缓冲(framebuffer)
- leetcode第一刷_Maximal Rectangle
- 实现字符串中单词顺序的逆置
- 数据库基本表创建 完整性约束 foreign Key
- 最近在折腾python,python在折腾我
- UNIX网络编程卷1(第三版)字节排序函数测试
- 金山-垃圾清理模块架构
- 协方差、协方差矩阵及其意义
- C语言数组的一些问题
- Android adb devices显示no permission
- 利用telnet命令检测远程主机端口是否正常开放
- 某互联网公司2014 Java工程师面试试题
- [dp] poj 1015 Jury Compromise
- 网络游戏分类及前景