package com.learn.bilibili.idconv;
public class IDConvUtil {
static String table = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
static int[] seqArray = new int[]{11, 10, 3, 8, 4, 6};
static long xOr = 177451812L;
static long xAdd = 8728348608L;
public static void main(String[] args) {
long av1 = 884740535L;
String bv1 = avEncode(av1);
System.out.printf("avid:%d <=> bvid: %s \n", av1, bv1);
String bv2 = "BV1rK4y187eg";
long av2= bvDecode(bv2);
System.out.printf("bvid:%s <==> avid:%d \n",bv2,av2);
}
private static String avEncode(long av1) {
long newAvId = (av1 ^ xOr) + xAdd;
char[] defaultBVId = new char[]{'B', 'V', '1', ' ', ' ', '4', ' ', '1', ' ', '7', ' ', ' '};
for (int i = 0; i < seqArray.length; i++) {
defaultBVId[seqArray[i]] = table.charAt((int) (newAvId / ((long) Math.pow(58, i)) % 58));
}
return new String(defaultBVId);
}
private static long bvDecode(String bv) {
long newAvId = 0L;
for (int i = 0; i < seqArray.length; i++) {
newAvId += table.indexOf(bv.charAt(seqArray[i])) * (long)Math.pow(58,i);
}
long avid = (newAvId - xAdd)^xOr;
return avid;
}
}
|