-2

How to get an unique(most of the time) ushort number from GUID, I have tried below code but since I am converting it to ushort so it is just ignoring the LSB hexadecimal values of GUID

static ushort GetId() {
            Guid guid = Guid.NewGuid();
            byte[] buffer = guid.ToByteArray();
            return BitConverter.ToUInt16(buffer, 0);
}

FYI: Somewhere in my code, I have a guid and I want to keep the corresponding ushort number.

Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35
  • `ushort` has only 65,536 possible values, how random/unique do you need it to be? – phuzi Nov 29 '18 at 11:58
  • 2
    If you want a unique 16 bit value the simplest way is to use a counter, the idea that you can borrow 16 bits of uniqueness from 128 bits of uniqueness is not going to work. – Alex K. Nov 29 '18 at 12:00
  • Possible duplicate of [How to generate a cryptographically secure random integer within a range?](https://stackoverflow.com/questions/42426420/how-to-generate-a-cryptographically-secure-random-integer-within-a-range) – mjwills Nov 29 '18 at 12:00
  • A GUID has 2^122 or 5,316,911,983,139,663,491,615,228,241,121,400,000 different possible values. An `ushort` has 65,535. See the difference? What you're asking for is impossible. Instead explain what problem you're trying to solve. – CodeCaster Nov 29 '18 at 12:02
  • **Why** do you want the `ushort` to be based on the GUID? Why not just generate a random `ushort` as per the duplicate? – mjwills Nov 29 '18 at 12:18
  • This is not likely to come to a good end. At least use uint, Guid.GetHashCode() tries to do a reasonable job of taking advantage of the bits in a guid that are unpredictable. – Hans Passant Nov 29 '18 at 12:32

1 Answers1

0

I have tried below code but since I am converting it to ushort so it is just ignoring the LSB hexadecimal values of GUID

Yes, this is correct and for good reason, you cannot store 128 bits of data in 16 bits of data.

Name                    Length (bytes)  Contents
---------------------------------------------------------------------------------------
time_low                            4   integer giving the low 32 bits of the time
time_mid                            2   integer giving the middle 16 bits of the time
time_hi_and_version                 2   4-bit "version" in the most significant bits, followed by the high 12 bits of the time
clock_seq_hi_and_res clock_seq_low  2   1-3 bit "variant" in the most significant bits, followed by the 13-15 bit clock sequence
node                                6   the 48-bit node id

If you want the last 16 bits (2 bytes, 4 hex values) Just reverse the array

Array.Reverse(buffer, 0, buffer.Length);
return BitConverter.ToUInt16(buffer, 0);

Note what you are doing is very suspect, and i truly think you need to rethink your design

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • 1
    Does this factor in the odd (to some people) way that `ToByteArray` returns its data? https://learn.microsoft.com/en-us/dotnet/api/system.guid.tobytearray?view=netframework-4.7.2#remarks – mjwills Nov 29 '18 at 12:32
  • @mjwills interesting i actually didn't expect that, however the last groups should be intact (as far i could gather) – TheGeneral Nov 29 '18 at 12:35