alphabet = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF'
def dec(x):
r = 0
for i, v in enumerate([11, 10, 3, 8, 4, 6]):
r += alphabet.find(x[v]) * 58**i
return (r - 0x2_0840_07c0) ^ 0x0a93_b324
def enc(x):
x = (x ^ 0x0a93_b324) + 0x2_0840_07c0
r = list('BV1**4*1*7**')
for v in [11, 10, 3, 8, 4, 6]:
x, d = divmod(x, 58)
r[v] = alphabet[d]
return ''.join(r)
用例:
In[52]: enc(170001)
Out[52]: 'BV17x411w7KC'
In[54]: dec('BV17x411w7KC')
Out[54]: 170001
C/C++
const char alphabet[] =
"fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
const char alphabet_tr[] =
"*************************************************"
"\r\x0c.\x1f+\x12(\x1c\x05*******6\x14\x0f\x08\'9-$*&3*14*5"
"\x07\x04\t2\n,\"\x06\x19\x01******\x1a\0358\x03\x18\x00/\x1b"
"\x16)\x10*\x0b%\x02#\x15\x11!\0360\0277 \x0e\x13";
unsigned long long bv2av(const char bv[13]) {
return (
alphabet_tr[bv[11]] +
alphabet_tr[bv[10]] * 58ull +
alphabet_tr[bv[ 3]] * 3364ull +
alphabet_tr[bv[ 8]] * 195112ull +
alphabet_tr[bv[ 4]] * 11316496ull +
alphabet_tr[bv[ 6]] * 656356768ull - 0x2084007c0ull
) ^ 0x0a93b324ull;
}
char* av2bv(unsigned long long av, char bv[13]) {
bv[0] = 'B'; bv[1] = 'V';
bv[2] = '1'; bv[5] = '4'; bv[7] = '1'; bv[9] = '7';
av = (av ^ 0x0a93b324ull) + 0x2084007c0ull;
bv[11] = alphabet[av % 58ull];
bv[10] = alphabet[(av /= 58ull) % 58ull];
bv[ 3] = alphabet[(av /= 58ull) % 58ull];
bv[ 8] = alphabet[(av /= 58ull) % 58ull];
bv[ 4] = alphabet[(av /= 58ull) % 58ull];
bv[ 6] = alphabet[(av /= 58ull) % 58ull];
return bv;
}
用例:
int main() {
char bv[13];
printf("BV17x411w7KC = av%d\n", bv2av("BV17x411w7KC"));
printf("av170001 = %s\n", av2bv(170001, bv));
return 0;
}
出处
如何看待 2020 年 3 月 23 日哔哩哔哩将稿件的「av 号」变更为「BV 号」? 原作者: mcfx