CSharpFeeds - All your C# feeds in one place.

Sponsors

Thursday, March 15, 2007

A shorter and URL friendly GUID

by post@madskristensen.dk (Mads Kristensen) via .NET Slave on 3/15/2007 4:17:33 PM

As a follow-up on my post from yesterday about generating shorter GUIDs, I’ve created a small helper class in C#. The class encodes a GUID into a 22 character long string and decodes the string back to the original GUID again.

That way you can save 10 characters from the original GUID and it is ASCII encoded so it will work perfectly in a URL as a query string or in ViewState.

It takes a standard GUID like this:

c9a646d3-9c61-4cb7-bfcd-ee2522c8f633

And converts it into this smaller string:

00amyWGct0y_ze4lIsj2Mw

using System;

publicstaticclass GuidEncoder
{
 publicstaticstring Encode(string guidText)
 {
  Guid guid =new Guid(guidText);
  return Encode(guid);
 }

 publicstaticstring Encode(Guid guid)
 {
  string enc = Convert.ToBase64String(guid.ToByteArray());
  enc = enc.Replace("/", "_");
  enc = enc.Replace("+", "-");
  return enc.Substring(0, 22);
 }

 publicstatic Guid Decode(string encoded)
 {
  encoded = encoded.Replace("_", "/");
  encoded = encoded.Replace("-", "+");
  byte[] buffer = Convert.FromBase64String(encoded +"==");
  returnnew Guid(buffer);
 }
}

It basically just converts a GUID into a base64 string and shortens it a bit.

email it!bookmark it!digg it!

Original Post: A shorter and URL friendly GUID

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us