-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathBase62Util.cs
73 lines (67 loc) · 2.32 KB
/
Base62Util.cs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Text;
namespace EasyTool
{
/// <summary>
/// Base62 编码解码工具类
/// </summary>
public static class Base62Util
{
// Base62 字符集,共 62 个字符
private static readonly char[] BASE62_CHARS =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();
/// <summary>
/// 将给定的整数转换为 Base62 编码字符串。
/// </summary>
/// <param name="number">要转换的整数</param>
/// <returns>转换后的 Base62 编码字符串</returns>
public static string Encode(long number)
{
if (number < 0)
{
throw new ArgumentOutOfRangeException(nameof(number), "Number must be non-negative.");
}
if (number == 0)
{
return BASE62_CHARS[0].ToString();
}
List<char> chars = new List<char>();
int targetBase = BASE62_CHARS.Length;
while (number > 0)
{
int index = (int)(number % targetBase);
chars.Add(BASE62_CHARS[index]);
number = number / targetBase;
}
chars.Reverse();
return new string(chars.ToArray());
}
/// <summary>
/// 将给定的 Base62 编码字符串转换为整数。
/// </summary>
/// <param name="str">要转换的 Base62 编码字符串</param>
/// <returns>转换后的整数</returns>
public static long Decode(string str)
{
if (string.IsNullOrEmpty(str))
{
throw new ArgumentException("String is null or empty.", nameof(str));
}
long result = 0;
int sourceBase = BASE62_CHARS.Length;
long multiplier = 1;
for (int i = str.Length - 1; i >= 0; i--)
{
int digit = Array.IndexOf(BASE62_CHARS, str[i]);
if (digit == -1)
{
throw new ArgumentException("Invalid character in string: " + str[i], nameof(str));
}
result += digit * multiplier;
multiplier *= sourceBase;
}
return result;
}
}
}