tags:

views:

339

answers:

5

Hi.

GUID is big random number show in a HEX basis. I want to show this number in a shorter format, lets say that based on the all letters and numbers. That is a 36 basis.

Lets say that: 2f1e4fc0-81fd-11da-9156-00036a0f876a will become as 3jEl9x6eZi.

Is there any 'ready' algorithm for this in .Net?

it need to be bidirectional.

Edit: using Base64 is even better solution. The only problem is Base64 contains / char which is not compatible to use in URI.

+1  A: 

Base64 is what I use, this will fix the issue with == and / and +

Fredou
+2  A: 

It's not possible as you presented it. You'd have to lose information:

>>> 16 ** len('2f1e4fc081fd11da915600036a0f876a')
340282366920938463463374607431768211456
>>> 36 ** len('3jEl9x6eZi')
3656158440062976

You'd need many more base 36 digits to cover all possible values. Why not just use base 64 instead? The result would be shorter (and I'm assuming this is the aim here) and there is a standard solution for that in .NET.

Mark Byers
The value `3jEl9x6eZi` is only for example, and it is not 'real' value.
Mendy
A: 

I think the closest that you will find is Base36 however it won't work with a GIUD type (only an Int16, Int32, or Int64).

Kane
+4  A: 

There is nothing built in for such a conversion. Something close that is built in is using base 64 encoding:

string base64 = Convert.ToBase64String(theGuid.ToByteArray())
Guffa
Good solution. But why each string ends with '==' ? `=` it's not a valid value in Base64 format.
Mendy
@Mendy: The = character is used to fill up the last character group. (It's specified in the RFC 1421 standard.)
Guffa
+3  A: 

Maybe this is what you need: ShortGuid - A shorter and url friendly GUID class in C#

Carlos Gutiérrez