views:

269

answers:

6

is it possible to generate short guid like in youtube (N7Et6c9nL9w) ? how can it be done? (i want to use it in web app)

A: 

Technically it's not a Guid. Youtube has a simple randomized string generator that you can probably whip up in a few minutes using an array of allowed characters and a random number generator.

Scott Muc
+2  A: 

You could use Base64:

string base64Guid = Convert.ToBase64String(Guid.NewGuid().ToByteArray());

That generates a string like E1HKfn68Pkms5zsZsvKONw==. Since a GUID is always 128 bits, you can omit the == that you know will always be present at the end and that will give you a 22 character string. This isn't as short as YouTube though.

bobbymcr
+6  A: 

Have a look at these links

ShortGuid - A shorter and url friendly GUID class in C#

A shorter and URL friendly GUID

astander
i saw this classes, but there not unique decisionand giud is still long
kusanagi
A: 

This id is probably not globally unique. GUID's should be globally unique as they include elements which should not occur elsewhere (the MAC address of the machine generating the ID, the time the ID was generated, etc.)

If what you need is an ID that is unique within your application, use a number fountain - perhaps encoding the value as a hexadecimal number. Every time you need an id, grab it from the number fountain.

If you have multiple servers allocating id's, you could grab a range of numbers (a few tens or thousands depending on how quickly you're allocating ids) and that should do the job. an 8 digit hex number will give you 4 billion ids - but your first id's will be a lot shorter.

Robert Christie
A: 

9 chars is not a Guid. Given that, you could use you could use the hexadecimal representation of an int, which gives you a 8 char string.

Update 1: Dunno why the above got a downvote, but for anyone wondering:

You can use an id you might already have. Also you can use .GetHashCode against different simple types and there you have a different int. You can also xor different fields. And if you are into it, you might even use a Random number - hey, u have well above 2.000.000.000+ possible values if you stick to the positives ;)

eglasius
down voters - please explain
Ron Klein
I also wonder that ... in any case, I just posted an update giving which gives extra reasons to do the opposite - upvote :)
eglasius
A: 

It might be not the best solution, but you can do something like that:

string shortUrl = System.Web.Security.Membership.GeneratePassword(11, 0);
philipproplesch