要求对以下datatrans函数编码的数据进行还原,只需要提供
datatrans函数的还原算法,datatrans函数中用到的bitArrToByteArr和toBytes函数不需要还原。
//现提供以下调用例子,能正确还原的请回贴。
datatrans([0, 1, 0], 6);
function datatrans(arr_result, int_unicode0_multi_count) {
//复制出新的数组
let arr_result_slice = arr_result.slice();
//保存原始数组长度
let arrRet_m = toBytes(0 | arr_result.length);
//添加4位1
arr_result_slice.push(1, 1, 1, 1);
let arrTmp_Ge = [];
let fun_fromCharCode = String.fromCharCode;
let fun_str_substr = "".substr;
let fun_str_indexOf = "".indexOf;
let arr_unicode_char256_U = [];
let arr_unicode_char256_g = [];
//获取unicode值=0-255的的字符数组
for (let i = 0; i < 256; i++) {
arr_unicode_char256_g[i] = String.fromCharCode.call(String, i);
arr_unicode_char256_U[i] = arr_unicode_char256_g[i];
} //Array(256)
//数组转成字符串
var str_unicode_char256_g = arr_unicode_char256_g.join("");
//把第一个字符循环加int_unicode0_multi_count次
var str_unicode_0_multi = "";
for (let d = 0; d < int_unicode0_multi_count; d++) {
str_unicode_0_multi += arr_unicode_char256_U[0];
}
//循环过程中arr_unicode_char256_g会扩充
let int_D_handle_index = 0;
let int_d_add_index = 1;
for (let i = 0; i < arr_result_slice.length; i++) {
var int_index = 255 & arr_result_slice[i];
let int_indexOf_index = fun_str_indexOf.call(str_unicode_char256_g, arr_unicode_char256_U[int_index]); //p=0
let int_indexOf_index_plus;
if (int_index === 255) {
int_indexOf_index_plus = str_unicode_char256_g.length;
} else {
//正常路径
int_indexOf_index_plus = fun_str_indexOf.call(str_unicode_char256_g, arr_unicode_char256_U[int_index + 1]); //1
}
let int_arr_unicode_char256_1_len = str_unicode_char256_g.length;
let float_divide_len = (int_d_add_index - int_D_handle_index) / int_arr_unicode_char256_1_len;
int_d_add_index = int_D_handle_index + float_divide_len * int_indexOf_index_plus;
int_D_handle_index += float_divide_len * int_indexOf_index;
console.log('%c ' + (i + 1) + '. ' + int_index, 'color: green; font-size: 14px; font-weight:Bold');
console.log('arr_len=' + str_unicode_char256_g.length, 'index=' + int_indexOf_index, 'index+=' + int_indexOf_index_plus,
'd=' + int_d_add_index, 'D=' + int_D_handle_index, 'float_divide_len=' + float_divide_len);
str_unicode_char256_g = fun_str_substr.call(str_unicode_char256_g, 0, int_indexOf_index_plus)
+ str_unicode_0_multi
+ fun_str_substr.call(str_unicode_char256_g, int_indexOf_index_plus);
let arr_push = []; //用于打印
let int_2d;
let int_2D;
let int_2d_shr0;
let int_2D_shr0;
do {
int_2d = 2 * int_d_add_index;
int_2D = 2 * int_D_handle_index;
int_2d_shr0 = int_2d >> 0;
int_2D_shr0 = int_2D >> 0;
if (int_2d_shr0 === int_2D_shr0) {
int_d_add_index = int_2d - int_2d_shr0;
int_D_handle_index = int_2D - int_2D_shr0;
arrTmp_Ge.push(int_2d_shr0);
arr_push.push(int_2d_shr0);
}
} while (int_2d_shr0 === int_2D_shr0)
console.log("2d_shr0=" + int_2d_shr0, "2D_shr0=" + int_2D_shr0, 'arr_push(' + arr_push.length + ')=' + arr_push);
}
//位数组转换为byte数组
arrRet_m = arrRet_m.concat(bitArrToByteArr(arrTmp_Ge));
console.log('转换结果:' + arrRet_m.join(', '));
return arrRet_m;
}
/**
* 将byte数组转换long数组,最后不足8位的特别处理
*/
function bitArrToByteArr(arr_byte) {
//4次循环
//把arrTmp7的8位一组转换成long保存到arrTmp10
//arrTmp10本来1个,加了4个,变成Arrar(5)
int_max_len = arr_byte.length - 8; //Array(40)
let arr_long = [];
let int_handle_index = 0;
let int_add_index = 0;
while(true) {
if (int_add_index) {
int_handle_index += 8;
}
int_add_index = 1;
if (!(int_max_len > int_handle_index)) {
break;
} else {
let long_arrTmp7_shl =
+ (arr_byte[int_handle_index] << 7)
+ (arr_byte[int_handle_index + 1] << 6)
+ (arr_byte[int_handle_index + 2] << 5)
+ (arr_byte[int_handle_index + 3] << 4)
+ (arr_byte[int_handle_index + 4] << 3)
+ (arr_byte[int_handle_index + 5] << 2)
+ (arr_byte[int_handle_index + 6] << 1)
+ (arr_byte[int_handle_index + 7]); //0
arr_long.push(long_arrTmp7_shl);
}
}
//循环后D=32
//把arrTmp7 32位后的值循环左移后push到arrTmp10
//把D
//把后面<=8的位另外转换
if (arr_byte.length > int_handle_index) {
let int_result_A = 0;
let int_ye_count = 0;
let int_w_add_index = 0;
while (true) {
if (int_w_add_index) {
int_ye_count += 1;
int_handle_index += 1;
}
int_w_add_index = 1;
if (!(8 > int_ye_count)) {
arr_long.push(int_result_A);
break;
} else {
int_result_A <<= 1;
if (arr_byte.length > int_handle_index) {
int_result_A += arr_byte[int_handle_index];
}
}
}
}
return arr_long;
}
/**
* 把整型转化成转化成数组,每7位转化为1位
*/
function toBytes(int_or0) {
let arrRet = void 0;
if (16384 > int_or0) {
let int_continue = 0;
int_or0 = 0 | int_or0;
if (128 > int_or0) {
let arrTmpTmp = [];
arrTmpTmp.push(int_or0);
arrRet = arrTmpTmp;
} else {
if (!int_continue) {
let int_or0_high7 = ((int_or0 - (int_or0 % 128)) / 128) & 127; //(商)
let int_or0_low7 = int_or0 % 128 + 128; //(余数+128)
let arrTmpTmp = [];
arrTmpTmp.push(int_or0_low7, int_or0_high7);
arrRet = arrTmpTmp;
}
}
} else {
let arrTmpTmp = [];
while (true) {
let int_or0_and127 = int_or0 & 127;
int_or0 >>= 7;
if (int_or0) {
int_or0_and127 = 128 | int_or0_and127;
}
arrTmpTmp.push(int_or0_and127);
if (!int_or0) {
break;
} else {
}
}
arrRet = arrTmpTmp;
}
return arrRet;
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!