Converting GUIDs to base-36

V

vector

I've got an application that generates GUIDs. A lot of GUIDs. Lots
of GUIDs that end up in files on disk, taking up space. I'd like to
continue using the Guid.NewGuid() function as my unique tag generator,
but I'd also like to compress them to base 36 strings, which should
retain their uniqueness but save me disk space. I've looked at
various base conversion functions, and haven't found a suitable one.
Further, I don't need an AnyBase converter. I need a very specific
thing: a base-16 string to base-36 string converter. Anything
additional would be a waste. Can somebody help me fill in the blanks
below?

static string MakeGuid36()
{
//make a new guid
Guid g = Guid.NewGuid();

//convert the guid to a base-16 string
string strGuid = g.ToString().Replace("-", "");

//convert the base-16 string to a base-36 string
??

//return the result
return strGuid;
}
 
D

D. Rush

Here's some C code to do what you want. You'll have to write a function to
handle 'words' but this is a fast way to do the character level conversion:

/***************************************************************************
*
return a base 36 character. v must be from 0 to 35.
****************************************************************************
/
static char base36(unsigned int v)
{
static char basechars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return basechars[v % 36];
}

You can see the whole file here:
http://www.koders.com/c/fidAE1343A83D838FCD04E5C19DB63750EFA1F47D78.aspx?s=base36
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top