using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sandbox;
public static partial class SandboxSystemExtensions
{
private const string CharList = "0123456789abcdefghijklmnopqrstuvwxyz";
private static char[] CharArray = CharList.ToCharArray();
///
/// Encode the given number into a Base36 string
///
public static string ToBase36( this T i ) where T : struct, IComparable, IComparable, IConvertible, IEquatable, IFormattable
{
var input = (long)Convert.ToDecimal( i );
if ( input < 0 ) throw new ArgumentOutOfRangeException( "input", input, "input cannot be negative" );
char[] clistarr = CharList.ToCharArray();
var result = new Stack();
while ( input != 0 )
{
result.Push( clistarr[input % 36] );
input /= 36;
}
return new string( result.ToArray() );
}
///
/// Decode the Base36 Encoded string into a number
///
///
///
public static long FromBase36( this string input )
{
var reversed = input.ToLower().Reverse();
long result = 0;
int pos = 0;
foreach ( char c in reversed )
{
result += CharList.IndexOf( c ) * (long)System.Math.Pow( 36, pos );
pos++;
}
return result;
}
}