forked from az64/mm-rando
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase36.cs
More file actions
57 lines (50 loc) · 1.62 KB
/
Base36.cs
File metadata and controls
57 lines (50 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Linq;
namespace MMRando
{
//-------------------------------------------------------------//
// https://www.stum.de/2008/10/20/base36-encoderdecoder-in-c/
// Edit: Slightly updated on 2011-03-29
/// <summary>
/// A Base36 De- and Encoder
/// </summary>
public static class Base36
{
private const string CharList = "0123456789abcdefghijklmnopqrstuvwxyz";
/// <summary>
/// Encode the given number into a Base36 string
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static String Encode(long input)
{
if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");
char[] clistarr = CharList.ToCharArray();
var result = new Stack<char>();
while (input != 0)
{
result.Push(clistarr[input % 36]);
input /= 36;
}
return new string(result.ToArray());
}
/// <summary>
/// Decode the Base36 Encoded string into a number
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static Int64 Decode(string input)
{
var reversed = input.ToLower().Reverse();
long result = 0;
int pos = 0;
foreach (char c in reversed)
{
result += CharList.IndexOf(c) * (long)Math.Pow(36, pos);
pos++;
}
return result;
}
}
}