[转帖]little-endian and big-endian

little,endian,and,big · 浏览次数 : 0

小编点评

The provided text explains the difference between big-endian and little-endian architectures and how they affect the order of bytes in a binary word. It also provides code examples for reading binary data from files using different encoding schemes. **Key Points:** * **Big-endian:** * Byte order: From left to right. * Representation of 0x12345678: 0x12 0x34 0x56 0x78 * **Little-endian:** * Byte order: From right to left. * Representation of 0x12345678: 0x78 0x56 0x34 0x12 **Example:** The code reads two binary files, `LittleEndian.txt` and `BigEndian.txt`, and prints the content of each file using a `PrintByteArray` method. **Code:** ```csharp private static byte[] ReadAsBinary(string path) { FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read); BinaryReader breader = new BinaryReader(fs); byte[] bytes = breader.ReadBytes(6); return bytes; } ``` **Output:** ``` Little Endian: FFFE Big Endian: FEFF ``` **Conclusion:** The code demonstrates that the order of bytes in a binary word can differ based on the endianness of the architecture. When reading binary data from files, it is important to consider the encoding scheme to ensure that the bytes are interpreted correctly.

正文

https://www.cnblogs.com/jenneyblog/archive/2010/06/29/1767330.html

 

Some computer architectures number bytes in a binary word from left to right, which is referred to as

big-endian. 

Other architectures number the bytes in a binary word from right to left, which is referred to as little-endian.

Using big-endian and little-endian methods, the number 0x12345678 would be stored as shown in the following table.

 

 

Byte order

Byte 0

Byte 1

Byte 2

Byte 3

Big-endian

0x12

0x34

0x56

0x78

Little-endian

0x78

0x56

0x34

0x12

例子:
下面C# code中有2个txt文件:LittleEndian.txt和BigEndian.txt,他们包含相同的文本内容:都是aa,但不同的编码方式,分别是UCS-2 Little Endian 和UCS-2 Big Endian.

示例中先分别用二进制的方式读出文件中的内容;再用StreamReader读出文件的内容

复制代码
 static void Main(string[] args)
        {
            byte[] leBytes = ReadAsBinary(".\\LittleEndian.txt");
            Console.WriteLine("Little Endian:");
            PrintByteArray(leBytes);
            Console.WriteLine();

            byte[] bigBytes = ReadAsBinary(".\\BigEndian.txt");
            Console.WriteLine("Big Endian:");
            PrintByteArray(bigBytes);
            Console.WriteLine();

            byte[] srByes;
            using (StreamReader reader = new StreamReader(".\\LittleEndian.txt", true))
            {
                srByes = Encoding.Unicode.GetBytes(reader.ReadToEnd());
            }
            Console.WriteLine("StreamReader Little Endian:");
            PrintByteArray(srByes);
            Console.WriteLine();
}

        private static byte[] ReadAsBinary(string path)
        {
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
            BinaryReader breader = new BinaryReader(fs);
            byte[] bytes = breader.ReadBytes(6);
            return bytes;
        }
复制代码

输出:

结论:

UCS-2 Little Endian编码的文件,其内容以FFFE开头;

UCS-2 Big Endian编码的文件,其内容以FEFF开头;

与[转帖]little-endian and big-endian相似的内容:

[转帖]little-endian and big-endian

https://www.cnblogs.com/jenneyblog/archive/2010/06/29/1767330.html Some computer architectures number bytes in a binary word from left to right, which

[转帖][译]ARM大小核架构白皮书

https://zhuanlan.zhihu.com/p/33411449 ARM big.LITTLE Processing with ARM Cortex-A15 & Cortex-A7 --Improving Energy Efficiency in High-Performance Mobi

[转帖]

Linux ubuntu20.04 网络配置(图文教程) 因为我是刚装好的最小系统,所以很多东西都没有,在开始配置之前需要做下准备 环境准备 系统:ubuntu20.04网卡:双网卡 网卡一:供连接互联网使用网卡二:供连接内网使用(看情况,如果一张网卡足够,没必要做第二张网卡) 工具: net-to

[转帖]

https://cloud.tencent.com/developer/article/2168105?areaSource=104001.13&traceId=zcVNsKTUApF9rNJSkcCbB 前言 Redis作为高性能的内存数据库,在大数据量的情况下也会遇到性能瓶颈,日常开发中只有时刻

[转帖]ISV 、OSV、 SIG 概念

ISV 、OSV、 SIG 概念 2022-10-14 12:29530原创大杂烩 本文链接:https://www.cndba.cn/dave/article/108699 1. ISV: Independent Software Vendors “独立软件开发商”,特指专门从事软件的开发、生产、

[转帖]Redis 7 参数 修改 说明

2022-06-16 14:491800原创Redis 本文链接:https://www.cndba.cn/dave/article/108066 在之前的博客我们介绍了Redis 7 的安装和配置,如下: Linux 7.8 平台 Redis 7 安装并配置开机自启动 操作手册https://ww

[转帖]HTTPS中间人攻击原理

https://www.zhihu.com/people/bei-ji-85/posts 背景 前一段时间,公司北京地区上线了一个HTTPS防火墙,用来监听HTTPS流量。防火墙上线之前,邮件通知给管理层,我从我老大那里听说这个事情的时候,说这个有风险,然后意外地发现,很多人原来都不知道HTTPS防

[转帖]关于字节序(大小端)的一点想法

https://www.zhihu.com/people/bei-ji-85/posts 今天在一个技术群里有人问起来了,当时有一些讨论(不完全都是我个人的观点),整理一下: 为什么网络字节序(多数情况下)是大端? 早年设备的缓存很小,先接收高字节能快速的判断报文信息:包长度(需要准备多大缓存)、地

[转帖]awk提取某一行某一列的数据

https://www.jianshu.com/p/dbcb7fe2da56 1、提取文件中第1列数据 awk '{print $1}' filename > out.txt 2、提取前2列的文件 awk `{print $1,$2}' filename > out.txt 3、打印完第一列,然后打

[转帖]awk 中 FS的用法

https://www.cnblogs.com/rohens-hbg/p/5510890.html 在openwrt文件 ar71xx.sh中 查询设备类型时,有这么一句, machine=$(awk 'BEGIN{FS="[ \t]+:[ \t]"} /machine/ {print $2}' /