zoukankan      html  css  js  c++  java
  • littleendian and bigendian

    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开头;

  • 相关阅读:
    2021.5.10-(叶子相似的树)
    2021.5.8-N皇后(回溯)
    2021.5.6-(雪糕最大数)
    2021.4.23刷题(回溯-全排列)
    可持久化动态图上树状数组维护01背包
    Infinite String Comparision
    第6章 操作系统 存储器管理(二)
    markdown
    操作系统 第6章 存储管理(一)
    操作系统 第五章 死锁 (二)
  • 原文地址:https://www.cnblogs.com/jenneyblog/p/1767330.html
Copyright © 2011-2022 走看看