Increment Guid in C#

后端 未结 4 792
一向
一向 2021-01-17 18:32

I have an application that has a guid variable which needs to be unique (of course). I know that statistically any guid should just be assumed to be unique, but due to dev/t

4条回答
  •  一个人的身影
    2021-01-17 19:00

    Possible solution -- I think this works (not really tested), but want a better solution.

    public static Guid Increment(this Guid value)
    {
        var bytes = value.ToByteArray();
        // Note that the order of bytes in the returned byte array is different from the string representation of a Guid value.
        //  Guid:       00112233-4455-6677-8899-aabbccddeeff
        //  byte array: 33 22 11 00 55 44 77 66 88 99 AA BB CC DD EE FF
        // So the byte order of the following indexes indicates the true low-to-high sequence
        if (++bytes[15] == 0) if (++bytes[14] == 0) if (++bytes[13] == 0) if (++bytes[12] == 0) if (++bytes[11] == 0) if (++bytes[10] == 0) // normal order
         if (++bytes[9] == 0) if (++bytes[8] == 0) // normal order
          if (++bytes[6] == 0) if (++bytes[7] == 0) // reverse order
           if (++bytes[5] == 0) if (++bytes[4] == 0) // reverse order
            if (++bytes[3] == 0) if (++bytes[2] == 0) if (++bytes[1] == 0) { ++bytes[0]; } // reverse order
        return new Guid(bytes);
    }
    

    Edit: here is the code I ended up using; props to the answers above for the general technique, although without the "unchecked" clause they both would throw exceptions in some cases. But I also tried to make the below as readable as possible.

    private static int[] _guidByteOrder = { 15, 14, 13, 12, 11, 10, 9, 8, 6, 7, 4, 5, 0, 1, 2, 3 };
    public static Guid NextGuid(this Guid guid)
    {
        var bytes = guid.ToByteArray();
        for (int i = 0; i < 16; i++)
        {
            var iByte = _guidByteOrder[i];
            unchecked { bytes[iByte] += 1; }
            if (bytes[iByte] != 0)
                return new Guid(bytes);
        }
        return Guid.Empty;
    }
    

提交回复
热议问题