11

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.

Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96
  • For base 64, you can substitute invalid characters with valid (but not already used) ones. IIRC, there's slightly less than 96 printable ASCII characters. –  Feb 14 '10 at 01:06
  • Look at my answer to see how I fixed the ==, / and + issue – Fredou Feb 14 '10 at 02:22

5 Answers5

13

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

Carlos Gutiérrez
  • 13,972
  • 5
  • 37
  • 47
9

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
  • 687,336
  • 108
  • 737
  • 1,005
2

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
  • 811,555
  • 193
  • 1,581
  • 1,452
1

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

Community
  • 1
  • 1
Fredou
  • 19,848
  • 10
  • 58
  • 113
1

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
  • 16,471
  • 11
  • 61
  • 86