Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a unique identifier that does not contain numbers?

I'm looking to use some sort of unique identifier within a .resx file but it does not allow the key to begin with a number. Rather than cycling through GUIDs until I get one that starts with a letter, I'm wondering if there's an alternative UID type that either does not contain numbers or would otherwise meet this requirement.

Any thoughts?

like image 213
Sinaesthetic Avatar asked Sep 05 '13 20:09

Sinaesthetic


People also ask

How do I make a global unique identifier?

Individuals and organizations can create GUIDs using a free GUID generator that is available online. An online generator constructs a unique GUID according to RFC 4122. When creating a GUID, users should note the timestamp, clock sequence and the node ID -- such as a Media Access Control (MAC) address.

How do I create a new GUID?

To Generate a GUID in Windows 10 with PowerShell, Type or copy-paste the following command: [guid]::NewGuid() . This will produce a new GUID in the output. Alternatively, you can run the command '{'+[guid]::NewGuid(). ToString()+'}' to get a new GUID in the traditional Registry format.

What is UUID generator?

A UUID (Universal Unique Identifier) is a 128-bit value used to uniquely identify an object or entity on the internet. Depending on the specific mechanisms used, a UUID is either guaranteed to be different or is, at least, extremely likely to be different from any other UUID generated until A.D. 3400.


3 Answers

If you just want to create a Guid that starts with a letter, you could do something like this:

var b = Guid.NewGuid().ToByteArray();
b[3] |= 0xF0;
return new Guid(b);

This will always generate a GUID that starts with the hex digit F.

To create a Guid that doesn't contain any numbers you could use something like this:

return new Guid(Guid.NewGuid().ToByteArray()
    .Select(b => (byte)(((b % 16) < 10 ? 0xA : b) | 
                        (((b >> 4) < 10 ? 0xA : (b >> 4)) << 4)))
    .ToArray());

This will test each hex digit (two per byte) and coerce it to A if it's less than A.


Both the above solutions generate real Guid objects, although the added restrictions do decrease the uniqueness of the resulting GUIDs to some degree (far more so in the second example). If you don't care about the output being actual GUIDs, you can simply remap the hex digits to something else and return the result a string, as others have suggested. FWIW, here's the shortest solution I can think of:

return String.Concat(Guid.NewGuid().ToString("N").Select(c => (char)(c + 17)));

This maps the hex digits 0 through 9 to the characters A through J, and the hex digits A - F to the characters r through w. It also generates a string without any hyphens. It For example:

Before: e58d0f329a2f4615b922ecf53dcd090a
After:  vFIuAwDCJrCwEGBFsJCCvtwFDutuAJAr

Of course, you could convert this to all upper or lower case if you don't like the mixed case here.

like image 56
p.s.w.g Avatar answered Oct 04 '22 12:10

p.s.w.g


How about generating a unique number and then prefixing it with a letter? So instead of

1234

You would use

a1234

As long as the algorithm you choose for the identifier guarantees a unique number, this should work just fine. It will also give you the ability to strip out the prefix and work with the identifier as a number again if need be.

like image 23
aleppke Avatar answered Oct 04 '22 13:10

aleppke


You can write and use a psuedorandom sequence generator. Here's one that gives the basic idea:

class RandomLetterSequence { 
    private static Random r; 
    private static char MinChar = (char) 0x0061; 
    private static char MaxChar = (char) 0x007A; 

    public static string RandomSequence() { 
        return RandomSequence(32);  
    }

    public static string RandomSequence(int length) { 
        if (r == null)
            r = new Random(); 
        var sb = new StringBuilder(); 
        for (int i = length; i >= 0; i--) { 
            sb.Append((char)(r.Next(MinChar, MaxChar))); 
        }
        return sb.ToString();
    }
}

With this implementation, there are 26^32 possible different sequences that are generated that conform to your requirements:

  • Similar to GUIDs in terms of collision rate (infinitesimally small)
  • Contains only letters
like image 33
jdphenix Avatar answered Oct 04 '22 12:10

jdphenix