大小端檢測方法(ARM 和linux系統)

來源:互聯網
上載者:User

http://blog.csdn.net/haojianno1/article/details/7925797

第一部份:檢測ARM或其他單片機

一、概念及詳解


在各種體系的電腦中通常採用的位元組儲存機制主要有兩種: big-endian和little-endian,即大端模式和小端模式。
先回顧兩個關鍵詞,MSB和LSB:
MSB:MoST Significant Bit ------- 最高有效位
LSB:Least Significant Bit ------- 最低有效位

大端模式(big-edian)

big-endian:MSB存放在最低端的地址上。

舉例,雙位元組數0x1234以big-endian的方式存在起始地址0x00002000中:
| data |<-- address
| 0x12 |<-- 0x00002000
| 0x34 |<-- 0x00002001
在Big-Endian中,對於bit序列中的序號編排方式如下(以雙位元組數0x8B8A為例):
bit | 0 1 2 3 4 5 6 7 | 8 9 10 11 12 13 14 15
------MSB----------------------------------LSB
val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 |
+--------------------------------------------+
= 0x8 B 8 A

小端模式(little-endian)

little-endian:LSB存放在最低端的地址上。

舉例,雙位元組數0x1234以little-endian的方式存在起始地址0x00002000中:
| data |<-- address
| 0x34 |<-- 0x00002000
| 0x12 |<-- 0x00002001
在Little-Endian中,對於bit序列中的序號編排和Big-Endian剛好相反,其方式如下(以雙位元組數0x8B8A為例):
bit | 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0
------MSB-----------------------------------LSB
val | 1 0 0 0 1 0 1 1 | 1 0 0 0 1 0 1 0 |
+---------------------------------------------+
= 0x8 B 8 A

二、數組在大端小端情況下的儲存:

以unsigned int value = 0x12345678為例,分別看看在兩種位元組序下其儲存情況,我們可以用unsignedchar buf[4]來表示value:
Big-Endian: 低地址存放高位,如下:
高地址
---------------
buf[3] (0x78) -- 低位
buf[2] (0x56)
buf[1] (0x34)
buf[0] (0x12) -- 高位
---------------
低地址
Little-Endian: 低地址存放低位,如下:
高地址
---------------
buf[3] (0x12) -- 高位
buf[2] (0x34)
buf[1] (0x56)
buf[0] (0x78) -- 低位
--------------
低地址

三、大端小端轉換方法:

Big-Endian轉換成Little-Endian如下:
#define BigtoLittle16(A) ((((uint16)(A) & 0xff00) >> 8) |
(((uint16)(A) & 0x00ff) << 8))
#define BigtoLittle32(A) ((((uint32)(A) & 0xff000000) >>24) |
(((uint32)(A) & 0x00ff0000) >> 8) |
(((uint32)(A) & 0x0000ff00) << 8) |
(((uint32)(A) & 0x000000ff) << 24))

四、大端小端檢測方法:(數組或聯合體(推薦))

如何檢查處理器是big-endian還是little-endian?
聯合體uniON的存放順序是所有成員都從低地址開始存放,利用該特性就可以輕鬆地獲得了CPU對記憶體採用Little-endian還是Big-endian模式讀寫。
int checkCPUendian()
{
union
{
unsigned int a;
unsigned char b;
}c;
c.a = 1;
return (c.b == 1);
}

/*return 1 : little-endian, return 0:big-endian*/


第二部份  檢測linux系統大小端

/* ex8-1.c */#include <sys/utsname.h>#include <unistd.h>#include <stdio.h>int main(){union{shortinum;char c[sizeof(short)];} un;struct utsnameuts;un.inum=0x0102;if(uname(&uts)<0){printf("Could not get host information .\n");return -1;}printf("%s -%s-%s:\n",uts.machine, uts.sysname, uts.release);if(sizeof(short)!=2){printf("sizeof short =%d\n", sizeof(short));return 0;}if(un.c[0]==1 && un.c[1]==2)printf("big_endian.\n");else if(un.c[0]==2 && un.c[1]==1)printf("little_endian.\n");elseprintf("unknown .\n");return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.