@diyaner/ding
Version:
dingiyan常用ts/js工具
65 lines • 1.76 kB
JavaScript
;
/*
* @copyright: ChengXuan
* @Author: ding
* @Date: 2022-10-29 15:14:19
* @LastEditors: ding
* @LastEditTime: 2022-10-29 15:41:08
* @Description: file content
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.hex2int = exports.toEightId = void 0;
/**
* 10位id卡号转换为8位id卡号
*
* @export
* @param {(string | number)} decNumber 十位id卡号
* @return {*}
*/
function toEightId(decNumber) {
let hexNumber = Number(decNumber).toString(16);
hexNumber = hexNumber.padStart(8, "0");
// 去掉首2位,2位转10进制就是8位id的前3位,4位转10进制就是id后5位
const two = hexNumber.substring(2, 4);
const three = hexNumber.substring(4);
let h2 = hex2int(two, 16).toString();
h2 = h2.padStart(3, "0");
let h4 = hex2int(three, 16).toString();
h4 = h4.padStart(5, "0");
const eightNumber = h2 + h4;
return eightNumber;
}
exports.toEightId = toEightId;
/**
* 其他进制进制转10进制
*
* 10进制转其他进制直接而toString(radix)即可。
*
* @export
* @param {string} hex 某进制的数值的字符串(字符串没有0x)
* @param {number} radix 进制 between 2-36
* @return {*}
*/
function hex2int(hex, radix = 16) {
var len = hex.length, a = new Array(len), code;
for (var i = 0; i < len; i++) {
code = hex.charCodeAt(i);
if (48 <= code && code < 58) {
code -= 48;
}
else {
code = (code & 0xdf) - 65 + 10;
}
a[i] = code;
}
return a.reduce((acc, c) => {
acc = radix * acc + c;
return acc;
}, 0);
}
exports.hex2int = hex2int;
exports.default = {
toEightId,
hex2int,
};
//# sourceMappingURL=number.js.map