代码之家  ›  专栏  ›  技术社区  ›  Matthew Layton

C#-为什么系统。Guid翻转字节数组中的字节?

  •  3
  • Matthew Layton  · 技术社区  · 7 年前

    System.Guid 翻转某些字节:

    new Guid(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
    [03020100-0504-0706-0809-0a0b0c0d0e0f]
    

    使用值为0到15的非顺序字节数组时

    new Guid(new byte[] {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15})
    [00010203-0405-0607-0809-0a0b0c0d0e0f]
    

    3 回复  |  直到 7 年前
        1
  •  5
  •   Community CDub    4 年前

    在上找到 Wikipedia

    其他系统,尤其是微软在其COM/OLE库中对UUID的编组,使用混合的endian格式,其中UUID的前三个组件是小endian,后两个是大endian。

        2
  •  1
  •   schnaader    7 年前

    第一个4字节块属于Int32值,接下来的2个块属于Int16值,由于 byte order . 也许你应该试试 other constructor 它将匹配的整数数据类型作为参数,并提供更直观的排序:

    Guid g = new Guid(0xA, 0xB, 0xC, 
                      new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7 } );
    Console.WriteLine("{0:B}", g);
    // The example displays the following output:
    //        {0000000a-000b-000c-0001-020304050607}
    
        3
  •  1
  •   Sergey Kalinichenko    7 年前

    source code of Guid.cs 要查看其背后的结构:

    // Represents a Globally Unique Identifier.
    public struct Guid : IFormattable, IComparable,
                         IComparable<Guid>, IEquatable<Guid> {
        //  Member variables
        private int         _a; // <<== First group,  4 bytes
        private short       _b; // <<== Second group, 2 bytes
        private short       _c; // <<== Third group,  2 bytes
        private byte       _d;
        private byte       _e;
        private byte       _f;
        private byte       _g;
        private byte       _h;
        private byte       _i;
        private byte       _j;
        private byte       _k;
        ...
    }
    

    如您所见,内部Guid由一个32位整数、两个16位整数和8个单独的字节组成。在…上 little-endian architectures int 还有两个 short 后面的按相反顺序存储。其余八个字节的顺序保持不变。