bilibili-bv-and-av-convert

bv av 互相转换

这是c++代码

此代码以WTFPL开源。

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
74
75
76
77
78
79
80
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
typedef unsigned long long ull;
const string table="fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
const ull xorinit=177451812;
const ull addinit=100618342136696320;
const int order[10]={11,10,3,8,4,6,2,9,5,7};
const int order2[10]={6,2,4,8,5,9,3,7,1,0};
ull powInt(ull x,ull n){
ull powN=1;
for(int i=0;i<n;i++){//the pow func
powN*=x;
}
return powN;
}
ull y(ull x,ull n){
ull powN=1;
for(int i=0;i<n;i++){//the pow func
powN*=58;
}
return (x/powN)%58;
}
int main(int argc, const char * argv[]) {
// insert code here...
string in;
cout<<"put the code here to convert:\n";
cin>>in;
if (in[0]=='a'||in[0]=='A') {//av to bv convert
ull numIn=0;
for(int i=2;i<in.length();i++){
numIn*=10;
numIn+=(in[i]-'1'+1);
}
numIn=numIn^xorinit;
numIn+=addinit;
ull ansTmp[10]={0};//ans in number version and without exchange
for (int i=0; i<10; i++) {
ansTmp[i]=y(numIn, i);
//cout<<ansTmp[i]<<endl;
}
ull ansTmp2[10]={0};//ans in number version and with exchange
for (int i=0; i<10; i++) {//exchange
ansTmp2[order[i]-2]=ansTmp[i];
}
cout<<"BV";
for (int i=0; i<10; i++) {//print the ans
cout<<table[ansTmp2[i]];
}
cout<<endl;
}else if (in[0]=='b'||in[0]=='B') {//bv to av convert
ull ansTmp[10]={0};//ans in number version and without exchange
for (int i=0; i<10; i++) {
for (int j=0; j<58; j++) {
if(in[i+2]==table[j]) {
ansTmp[i]=j;
goto exit1;//break
}
}
exit1:;
//cout<<ansTmp[i]<<endl;
}
for (int i=0; i<10; i++) {
ansTmp[i]*=powInt(58,order2[i]);
//cout<<ansTmp[i]<<endl;
}
ull ans=0-addinit;
for(int i=0;i<10;i++){//find the sum
ans+=ansTmp[i];
}
cout<<"av"<<(ans^xorinit)<<endl;//print the ans
return 0;
}else {//neither av nor bv
cout<<"error\n";
return 0;
}

}

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import SwiftUI
import Foundation

let table = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF"
let Chartable = Array(table)
let xorinit: UInt64 = 177451812
let addinit: UInt64 = 100618342136696320
let order = [11, 10, 3, 8, 4, 6, 2, 9, 5, 7]
let order2 = [6, 2, 4, 8, 5, 9, 3, 7, 1, 0]
let magicnumber = UInt64(0x80000000)

func powInt(_ x: UInt64, _ n: Int) -> UInt64 {
var powN: UInt64 = 1
for _ in 0..<n {
powN *= x
}
return powN
}

func y(_ x: UInt64, _ n: Int) -> UInt64 {
var powN: UInt64 = 1
for _ in 0..<n {
powN *= 58
}
return (x / powN) % 58
}

func conv(input:String)->String {
var ansOut=""
var isNew=false
let inString = input

if inString.hasPrefix("a") || inString.hasPrefix("A") {
var numIn: UInt64 = 0
let startIndex = inString.index(inString.startIndex, offsetBy: 2)
for char in inString[startIndex...] {
numIn *= 10
if let digit = char.wholeNumberValue {
numIn += UInt64(digit)
}
}

if numIn>1000000000{
isNew=true
}

numIn = numIn ^ xorinit
numIn += addinit
if isNew {
numIn-=magicnumber
}

var ansTmp = Array(repeating: UInt64(0), count: 10)
for i in 0..<10 {
ansTmp[i] = y(numIn, i)
}
var ansTmp2 = Array(repeating: UInt64(0), count: 10)
for i in 0..<10 {
ansTmp2[order[i] - 2] = ansTmp[i]
}
ansOut="BV"
for i in ansTmp2 {
ansOut+=String(Chartable[Int(i)])
}
} else if inString.hasPrefix("b") || inString.hasPrefix("B") {
var ansTmp = Array(repeating: UInt64(0), count: 10)
for i in 0..<10 {
if let j = table.firstIndex(of: inString[inString.index(inString.startIndex, offsetBy: i + 2)]) {
ansTmp[i] = UInt64(table.distance(from: table.startIndex, to: j))
}
}
for i in 0..<10 {
ansTmp[i] *= powInt(58, order2[i])
}
var ans: UInt64 = 0
for i in ansTmp {
ans += i
}
if ans<addinit {
ans+=magicnumber

}
ans -= addinit
ansOut="av\(ans ^ xorinit)"
} else {
ansOut="Error: Input is neither av nor bv"
}
return ansOut
}

致谢:

  1. av转bv号过程 https://www.bilibili.com/BV1N741127Tj

  2. bv转av https://www.bilibili.com/BV1R7411y7kw

  3. 知乎大佬 https://www.zhihu.com/question/381784377/answer/1099438784

  4. 知乎大佬https://www.zhihu.com/question/381784377

  5. 这里说一下,新的bv号如果按照旧的算法转换的话会产生一个负的av号,这时候应该加0x80000000得到新的av号

  6. swift代码大部分都是chatgpt写的

  7. WTFPL

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    Full license text:

    DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
    Version 2, December 2004

    Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

    Everyone is permitted to copy and distribute verbatim or modified
    copies of this license document, and changing it is allowed as long
    as the name is changed.

    DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

    0. You just DO WHAT THE FUCK YOU WANT TO.