Fitzchak Yitzchaki
Fitzchak Yitzchaki

Reputation: 9163

Show a GUID in 36 letters format

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.

Upvotes: 11

Views: 10997

Answers (5)

Fredou
Fredou

Reputation: 20120

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

Upvotes: 1

Carlos Gutiérrez
Carlos Gutiérrez

Reputation: 14292

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

Upvotes: 14

Guffa
Guffa

Reputation: 700630

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())

Upvotes: 9

Kane
Kane

Reputation: 16812

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).

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838816

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.

Upvotes: 2

Related Questions