XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/bilibili-API-collect

添加AV、BV互转的Java实现 (#478)

91e9e4b
7rikka <takanashirikkax@gmail.com>
提交于

代码差异

1 个文件 +43 -2
Modified other/bvid_desc.md +43 -2
@@ -59,9 +59,9 @@
59 59
60 60 ## 转换程序
61 61
62 使用Python、C以及TypeScript作为示例,欢迎社区提交更多例程
62 使用Python、C、TypeScript以及Java作为示例,欢迎社区提交更多例程
63 63
64 ### python
64 ### Python
65 65
66 66 ```python
67 67 table = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF' # 码表
@@ -197,3 +197,44 @@ console.log(bvcode.av2bv(170001));
197 197 BV17x411w7KC
198 198 170001
199 199 ```
200
201 ### Java
202
203 ```java
204 /**
205 * 算法来自:https://www.zhihu.com/question/381784377/answer/1099438784
206 */
207 public class Util {
208 private static final String TABLE = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
209 private static final int[] S = new int[]{11, 10, 3, 8, 4, 6};
210 private static final int XOR = 177451812;
211 private static final long ADD = 8728348608L;
212 private static final Map<Character, Integer> MAP = new HashMap<>();
213
214 static {
215 for (int i = 0; i < 58; i++) {
216 MAP.put(TABLE.charAt(i), i);
217 }
218 }
219
220 public static String aidToBvid(int aid) {
221 long x = (aid ^ XOR) + ADD;
222 char[] chars = new char[]{'B', 'V', '1', ' ', ' ', '4', ' ', '1', ' ', '7', ' ', ' '};
223 for (int i = 0; i < 6; i++) {
224 int pow = (int) Math.pow(58, i);
225 long i1 = x / pow;
226 int index = (int) (i1 % 58);
227 chars[S[i]] = TABLE.charAt(index);
228 }
229 return String.valueOf(chars);
230 }
231
232 public static int bvidToAid(String bvid) {
233 long r = 0;
234 for (int i = 0; i < 6; i++) {
235 r += MAP.get(bvid.charAt(S[i])) * Math.pow(58, i);
236 }
237 return (int) ((r - ADD) ^ XOR);
238 }
239 }
240 ```