Skip to content

Instantly share code, notes, and snippets.

@musoftware
Forked from rastislavcore/likeyoutubeID.cs
Created November 10, 2016 09:44
Show Gist options
  • Save musoftware/a28e38dfe9bf266dd3aa061074c1ffc7 to your computer and use it in GitHub Desktop.
Save musoftware/a28e38dfe9bf266dd3aa061074c1ffc7 to your computer and use it in GitHub Desktop.
C#
class ShortId
{
public static readonly string Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static decimal BcPow(double a, double b)
{
return Math.Floor((decimal)Math.Pow(a, b));
}
public static ulong Decode(string value, int pad = 0)
{
value = value.ReverseString();
var len = value.Length - 1;
ulong result = 0;
for (int t = len; t >= 0; t--)
{
var bcp = (ulong)BcPow(Alphabet.Length, len - t);
result += (ulong)Alphabet.IndexOf(value[t]) * bcp;
}
if (pad > 0)
{
result -= (ulong)BcPow(Alphabet.Length, pad);
}
return result;
}
public static string Encode(byte[] value, int startIndex = 0, int pad = 0)
{
return Encode(BitConverter.ToUInt64(value, startIndex), pad);
}
public static string Encode(Guid guid, int pad = 0)
{
var bytes = guid.ToByteArray();
var first = Encode(bytes, 0, pad);
var second = Encode(bytes, 8, pad);
return first + second;
}
public static string Encode(ulong value, int pad = 0)
{
var result = string.Empty;
if (pad > 0)
{
value += (ulong)BcPow(Alphabet.Length, pad);
}
for (var t = (value != 0 ? Math.Floor(Math.Log(value, Alphabet.Length)) : 0); t >= 0; t--)
{
var bcp = (ulong)BcPow(Alphabet.Length, t);
var a = ((ulong)Math.Floor((decimal)value / (decimal)bcp)) % (ulong)Alphabet.Length;
result += Alphabet[(int)a];
value = value - (a * bcp);
}
return result.ReverseString();
}
private static string ReverseString(this string value)
{
char[] arr = value.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment