写一个程序判断机器的的数据表示采用的是大端还是小端?
来源:互联网 发布:淘宝店招尺寸是多少 编辑:程序博客网 时间:2024/04/25 16:34
写一个程序判断机器的的数据表示采用的是大端还是小端?
分析:大小端问题:计算机存储数据的最基本的单位是byte(字节,8bit)。
c语言中,32位机的情形下:
char 1byte;
short 2bytes;
int 4bytes;
对于 short, int 这种多字节的数据类型,计算机内部是如何表示呢?
计算机对多字节数据类型数据的存储分为大端模式和小端模式:
小端模式:低位字节,在低地址;高位字节,在高地址。
大端模式: 低位字节,在高地址;高位字节,在低地址。
因此:对于同一个整数,其存储表示在大小端模式下不一定相同;相同的存储内容,在大端模式和小端模式下,表示的也未必是同一个整数。
例如:unsigned int a = 0x12345678;
小端模式下:其第一个字节 = 0x78; 第二个字节= 0x56; 第三个字节= 0x34;第四个字节=0x12;
大端模式下:其第一个字节 = 0x12; 第二个字节 = 0x34; 第三个字节=0x56; 第四个字节= 0x78;
以上四个字节的编号,是从低地址,到高地址编号;
在网络编程中,网络传递的字节序,是大端模式;通常需要大小端的转化:
常用的转化函数有:
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
如何判断一个机器采用的大端模式还是小端模式呢? 可以根据大小端模式的性质,实现编码:代码如下:
#include <stdio.h>#include <assert.h>int isLittleEndian();int isLittleEndian_1();int main(int argc, char *argv[]) { assert(isLittleEndian() == isLittleEndian_1() ); if(isLittleEndian() ) { printf("Little Endian\n"); }else { printf("Big Endian\n"); } if(isLittleEndian_1() ) { printf("Little Endian\n"); }else { printf("Big Endian\n"); } return 0;}int isLittleEndian(){ unsigned int a = 0x12345678; unsigned char *p = (unsigned char *) &a; return *p == (unsigned char) a;}typedef union { unsigned char byte; unsigned int dword;} char_or_int;int isLittleEndian_1() { char_or_int endian; endian.dword = 0x12345678; return endian.byte == 0x78;}
[wzb@embedded endian]$ man htonl | catBYTEORDER(3) Linux Programmer’s Manual BYTEORDER(3)NAME htonl, htons, ntohl, ntohs - convert values between host and network byte orderSYNOPSIS #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 i80x86 the host byte order is Least Significant Byte first, whereas the network byte order, as used on the Internet, is Most Sig- nificant Byte first.CONFORMING TO POSIX.1-2001. Some systems require the inclusion of <netinet/in.h> instead of <arpa/inet.h>.SEE ALSO gethostbyname(3), getservent(3)BSD 1993-04-15 BYTEORDER(3)[wzb@embedded endian]$
- 写一个程序判断机器的的数据表示采用的是大端还是小端?
- 判断一个机器使用的是大端存储还是小端存储
- 一个简单的程序教你检测你的机器是大端还是小端?
- 题目:如何判断一个机器是大端还是小端
- 判断一个机器是大端存储还是小端存储
- 写一个函数判断系统是大端还是小端
- 写一个函数判断存储空间是大端还是小端
- 判断机器是大端还是小端
- 你的机器是大端还是小端?
- 写一个程序判断当前系统存储模式是大端还是小端
- 如何判断自己的电脑是大端还是小端?
- 如何确定一个机器的字节顺序是大端还是小端
- 如何确定一个机器的字节顺序是大端还是小端
- 如何确定一个机器的字节顺序是大端还是小端
- 笔试面试7 如何判断一个系统的存储方式是大端还是小端
- 判断大端还是小端的代码
- 关于大小端的简单介绍以及使用C写一个函数判断当前系统是大端存储还是小端存储
- 小端法与大端法及验证机器小端还是大端的验证程序
- 数制转换(c语言)
- 语言微博营销。
- 【转载】Beam Search Algorithm (Draft by Andrew Jungwirth)
- 大数的加法
- 最新伤感情侣日志发布:用微笑去掩饰自己内心的伤悲
- 写一个程序判断机器的的数据表示采用的是大端还是小端?
- Java_乔晓松_Session技术-会话管理
- C++ 二叉搜索树 模板
- unicode gbk编码的相互转换 (jsp)
- Linux下USB子系统软件结构
- Opencv在VC6.0中的配置
- 使用T4为数据库自动生成实体类
- C++实现委托与事件代理
- How input events are dispatched in Android?