Files
kami_itunes_june/AppleBatch_June/ShareCodeUtils.cs
2024-07-22 00:43:14 +08:00

114 lines
2.4 KiB
C#

using System;
using System.Text;
namespace AppleBatch_June
{
public class ShareCodeUtils
{
private static char[] BASE = new char[32]
{
'H', 'V', 'E', '8', 'S', '2', 'D', 'Z', 'X', '9',
'C', '7', 'P', '5', 'I', 'K', '3', 'M', 'J', 'U',
'F', 'R', '4', 'W', 'Y', 'L', 'T', 'N', '6', 'B',
'G', 'Q'
};
private static char SUFFIX_CHAR = 'A';
private static int BIN_LEN = BASE.Length;
private static int CODE_LEN = 4;
public static string idToCode(long id)
{
char[] array = new char[BIN_LEN];
int num = BIN_LEN;
while (id / BIN_LEN > 0L)
{
int num2 = (int)(id % BIN_LEN);
array[--num] = BASE[num2];
id /= BIN_LEN;
}
array[--num] = BASE[(int)(id % BIN_LEN)];
string text = new string(array, num, BIN_LEN - num);
int length = text.Length;
if (length < CODE_LEN)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(SUFFIX_CHAR);
Random random = new Random();
for (int i = 0; i < CODE_LEN - length - 1; i++)
{
stringBuilder.Append(BASE[random.Next(BIN_LEN)]);
}
text += stringBuilder.ToString();
}
return text;
}
public static string idToNumber(long id, int leng)
{
char[] array = new char[BIN_LEN];
int num = BIN_LEN;
while (id / BIN_LEN > 0L)
{
int num2 = (int)(id % BIN_LEN);
array[--num] = BASE[num2];
id /= BIN_LEN;
}
array[--num] = BASE[(int)(id % BIN_LEN)];
string text = new string(array, num, BIN_LEN - num);
int length = text.Length;
if (length < leng)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(SUFFIX_CHAR);
Random random = new Random();
for (int i = 0; i < leng - length - 1; i++)
{
stringBuilder.Append(BASE[random.Next(BIN_LEN)]);
}
text += stringBuilder.ToString();
}
string text2 = "";
char[] array2 = text.ToCharArray();
foreach (char c in array2)
{
text2 += (int)c;
}
return text2;
}
public static long codeToId(string code)
{
try
{
char[] array = code.ToCharArray();
long num = 0L;
for (int i = 0; i < array.Length; i++)
{
int num2 = 0;
for (int j = 0; j < BIN_LEN; j++)
{
if (array[i] == BASE[j])
{
num2 = j;
break;
}
}
if (array[i] == SUFFIX_CHAR)
{
break;
}
num = ((i <= 0) ? num2 : (num * BIN_LEN + num2));
}
return num;
}
catch (Exception)
{
return 0L;
}
}
}
}