哔哩哔哩 BV、AV互转 C#版
从JAVA移植的(从python移植的B站AV号BV号互转算法源码)//套娃
引用@ty1937
https://blog.csdn.net/ty1937/article/details/105080093?
private static string table = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
private static Dictionary<string, Int64> b2aDic = new Dictionary<string, Int64>();
private static Dictionary<Int64, string> a2bDic = new Dictionary<Int64, string>();
static int[] ss = { 11, 10, 3, 8, 4, 6, 2, 9, 5, 7 };
static Int64 xor = 177451812;
static Int64 add = 8728348608L;
/// <summary>
/// 泡我(依赖性)
/// </summary>
/// <param name="a">英文字母的第一个字母</param>
/// <param name="b">英文字母的第二个字幕</param>
/// <returns>想要的结果(废话)</returns>
public static long Power(int a, int b)
{
Int64 power = 1;
for (int i = 0; i < b; i++)
{
power *= a;
}
return power;
}
/// <summary>
/// BV号转av号
/// </summary>
/// <param name="str">带有BV前缀的编号</param>
/// <returns>带有小写av前缀的编号</returns>
public static string BV2AV(string str)
{
try
{
str = str.Trim();
b2aDic.Clear();
Int64 r = 0;
for (int i = 0; i < 58; i++)
{
string s1 = table.Substring(i, 1);
b2aDic.Add(s1, i);
}
for (int i = 0; i < 6; i++)
{
r = r + b2aDic[str.Substring(ss[i], 1)] * Power(58, i);
}
return "av" + ((r - add) ^ xor);
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
return "Error";
throw;
}
}
/// <summary>
/// av号转BV号
/// </summary>
/// <param name="str">带有小写av前缀的编号</param>
/// <returns>带有大写BV前缀的编号</returns>
public static string AV2BV(string str)
{
try
{
str = str.Trim();
a2bDic.Clear();
string[] strs = { "av" };
string[] sss = (str.Split(strs, StringSplitOptions.RemoveEmptyEntries));
Int64 s = Convert.ToInt64(sss[0]);
StringBuilder sb = new StringBuilder("BV1 4 1 7 ");
s = (s ^ xor) + add;
for (int i = 0; i < 58; i++)
{
string s1 = table.Substring(i, 1);
a2bDic.Add(i, s1);
}
for (int i = 0; i < 6; i++)
{
string r = a2bDic[(s / Power(58, i) % 58)];
int index = Convert.ToInt32(ss[i].ToString());
for (int j = 0; j < sb.Length; j++)
{
sb[index] = r.ToCharArray()[0];
}
}
return sb.ToString();
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
return "Error";
throw;
}
}
花了点时间从python移植过来的JAVA移植过来的(套娃)。
java的substring和replace和C#的重载略不同
两个方法依赖Power方法,此为静态工具类