123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683 |
- /**
-
- * ArrayBuffer转16进制字符串
-
- */
- function arrayBuffer2HexString(buf) {
- var out = "";
- var u8a = new Uint8Array(buf);
- var single;
- for (
- var i = 0; i < u8a.length; i++) {
- single = u8a[i].toString(16)
- while (single.length < 2) single = "0".concat(single);
- out += single;
- }
- return out;
- }
- /**
-
- * 1、字符串转换为十六进制
-
- * 主要使用 charCodeAt()方法,此方法返回一个字符的 Unicode 值,该字符位于指定索引位置。
-
- */
- function stringToHex(str) {
- var val =
- "";
- for (
- var i =
- 0; i < str.length; i++) {
- val += str.charCodeAt(i).toString(
- 16);
- }
- return val;
- }
- function filterChinese(str) {
- var tmp = str.split(
- ''),
- arr = [];
- for (
- var i =
- 0, c = tmp.length; i < c; i++) {
- var s = tmp[i].charCodeAt();
- if (s >
- 0 && s <
- 127) {
- arr.push(tmp[i]);
- }
- }
- return arr;
- };
- // function strToHex(hex, isFilterChinese) {
- // hex = isFilterChinese ? filterChinese(hex).join(
- // '') : convertChinese(hex).join(
- // '');
- // //清除所有空格
- // hex = hex.replace(/\s/g,
- // "");
- // //若字符个数为奇数,补一个空格
- // hex += hex.length % 2 != 0 ? " " : "";
- // var c = hex.length / 2,
- // arr = [];
- // for (
- // var i = 0; i < c; i++) {
- // arr.push(parseInt(hex.substr(i * 2, 2), 16));
- // }
- // return arr;
- // };
- /**
-
- * 16进制字符串转ArrayBuffer
-
- */
- function hexString2ArrayBuffer(hexStr) {
- var count = hexStr.length / 2;
- let buffer = new ArrayBuffer(count);
- let dataView = new DataView(buffer);
- for (
- var i = 0; i < count; i++) {
- var curCharCode = parseInt(hexStr.substr(i * 2, 2), 16);
- dataView.setUint8(i, curCharCode);
- }
- return buffer;
- }
- /**
-
- * 字符串转为ArrayBuffer对象,参数为字符串
-
- */
- function string2ArrayBuffer(str) {
- var buf = new ArrayBuffer(str.length * 2);
- // 每个字符占用2个字节
- var bufView = new Uint8Array(buf);
- for (var i = 0, strLen = str.length; i < strLen; i++) {
- bufView[i] = str.charCodeAt(i);
- }
- return buf;
- }
- function isArray(arr) {
- return Object.prototype.toString.call(arr) === '[object Array]';
- };
- function strToByte(str) {
- var tmp = str.split(''),
- arr = [];
- for (var i = 0, c = tmp.length; i < c; i++) {
- var j = encodeURI(tmp[i]);
- if (j.length == 1) {
- arr.push(j.charCodeAt());
- } else {
- var b = j.split('%');
- for (var m = 1; m < b.length; m++) {
- arr.push(parseInt('0x' + b[m]));
- }
- }
- }
- return arr;
- };
- function convertChinese(str) {
- var tmp = str.split(''),
- arr = [];
- for (var i = 0, c = tmp.length; i < c; i++) {
- var s = tmp[i].charCodeAt();
- if (s <= 0 || s >= 127) {
- arr.push(s.toString(16));
- } else {
- arr.push(tmp[i]);
- }
- }
- return arr;
- };
- // function filterChinese(str) {
- // var tmp = str.split(''),
- // arr = [];
- // for (
- // var i = 0, c = tmp.length; i < c; i++) {
- // var s = tmp[i].charCodeAt();
- // if (s > 0 && s < 127) {
- // arr.push(tmp[i]);
- // }
- // }
- // return arr;
- // };
- // function strToHex(hex, isFilterChinese) {
- // hex = isFilterChinese ? filterChinese(hex).join('') : convertChinese(hex).join('');
- // //清除所有空格
- // hex = hex.replace(/\s/g, "");
- // //若字符个数为奇数,补一个空格
- // hex += hex.length % 2 != 0 ? " " : "";
- // var c = hex.length / 2,
- // arr = [];
- // for (var i = 0; i < c; i++) {
- // arr.push(parseInt(hex.substr(i * 2, 2), 16));
- // }
- // return arr;
- // };
- function padLeft(s, w, pc) {
- if (pc == undefined) {
- pc = '0';
- }
- for (
- var i = 0, c = w - s.length; i < c; i++) {
- s = pc + s;
- }
- return s;
- };
- function toString(arr, isReverse) {
- if (typeof isReverse == 'undefined') {
- isReverse = true;
- }
- var hi = arr[0],
- lo = arr[1];
- return padLeft((isReverse ? hi + lo * 0x100 : hi * 0x100 + lo).toString(16).toUpperCase(), 4, '0');
- };
- /**
-
- * 16进制字符串异或处理
-
- *
-
- * @param str1
-
- * @param str2
-
- * @return
-
- */
- function stringXor(str1, str2) {
- if (!str1 && !str2) {
- return "";
- }
- if (!str1 && str2) {
- return str2;
- }
- if (str1 && !str2) {
- return str1;
- }
- var longStr;
- var shortStr;
- if (str1.length >= str2.length) {
- longStr = str1;
- shortStr = str2;
- } else {
- longStr = str2;
- shortStr = str1;
- }
- var count = parseInt(shortStr.length /
- 2);
- var leftCount = longStr.length - shortStr.length;
- var resultStr = "";
- if (leftCount > 0) {
- resultStr += longStr.substr(0, leftCount);
- }
- for (
- var i = 0; i < count; i++) {
- var shortCharCode = parseInt(shortStr.substr(i * 2, 2), 16);
- var longCharCode = parseInt(longStr.substr(leftCount + i * 2, 2), 16);
- var resultCode = shortCharCode ^ longCharCode;
- var single = resultCode.toString(
- 16);
- while (single.length <
- 2) single =
- "0".concat(single);
- resultStr += single;
- }
- return resultStr.toUpperCase();
- }
- /**
-
- * 指令两个16进制字符串异或处理
-
- *
-
- * @param command
-
- * @param secretKey
-
- * @return
-
- */
- function getSecretEncoding(command, secretKey) {
- if (!command || !secretKey) {
- return "";
- }
- var secretLength = secretKey.length;
- var length = parseInt(command.length / secretLength);
- console.log(`command(${command.length })/secretLength(${secretLength }) = ${length }`);
- var resultCmd = "";
- console.log(`secretKey(${secretKey.length }):${secretKey }`);
- for (
- var i = 0; i < length; i++) {
- var part = command.substr(i * secretLength, secretLength);
- resultCmd += stringXor(part, secretKey);
- console.log(`part${i }:${stringXor(part, secretKey) }`);
- }
- var lastLen = command.length % secretLength;
- if (lastLen > 0) {
- console.log(`lastCMD:${command.substr(command.length - lastLen, lastLen) }`);
- console.log(`lastSecretKey:${secretKey.substr( 0, lastLen) }`);
- var lastPart = command.substr(command.length - lastLen, lastLen);
- var lastCmd = stringXor(lastPart, secretKey.substr(0, lastLen));
- resultCmd += lastCmd;
- console.log(`lastPart:${lastCmd }`);
- }
- return resultCmd;
- }
- /**
-
- * 2、十六进制转换为字符串
-
- *主要使用 fromCharCode()方法,此方法将 Unicode 码转换为与之对应的字符。
-
- */
- function hexToString(str) {
- var val = "";
- var arr = str.split(",");
- for (var i = 0; i < arr.length; i++) {
- val += arr[i].fromCharCode(i);
- }
- return val;
- }
- /**
-
- * 获取随机长度16进制字符串
-
- */
- function getRamNumber(length) {
- var result = '';
- for (
- var i = 0; i < length; i++) {
- result += Math.floor(Math.random() * 16).toString(16);
- //获取0-15并通过toString转16进制
- }
- //默认字母小写,手动转大写
- return result.toUpperCase();
- //另toLowerCase()转小写
- }
- /**
-
- * 得到BCD码时间字符串
-
- *
-
- * @param datetime
-
- * @return
-
- */
- function getBCDTime(datetime, needWeek) {
- if (typeof datetime == 'undefined') {
- datetime = new Date();
- }
- if (typeof needWeek == 'undefined') {
- needWeek = true;
- }
- var year = datetime.getFullYear() - 2000;
- //获取年份,从2000年开始计算
- if (year < 0) year = 0;
- // 不允许小于2000年的年份出现
- var month = datetime.getMonth() + 1;
- //获取月份 0-11 所以需要加1
- var day = datetime.getDate();
- //获取日
- var hour = datetime.getHours();
- //小时
- var minute = datetime.getMinutes();
- //分
- var second = datetime.getSeconds();
- //秒
- if (needWeek) {
- var dayOfWeek = datetime.getDay();
- //一周的第几天 0-6
- return formatNumber(year) + formatNumber(month) + formatNumber(day) + formatNumber(dayOfWeek)
- +
- formatNumber(hour) + formatNumber(minute) + formatNumber(second);
- // 得到BCD码的时间字符串
- } else {
- return formatNumber(year) + formatNumber(month) + formatNumber(day)
- +
- formatNumber(hour) + formatNumber(minute) + formatNumber(second);
- // 得到BCD码的时间字符串
- }
- }
- function formatNumber(n) {
- n = n.toString()
- return (n[1] ? n : '0' + n) + "";
- }
- /**
-
- * 判断一个字符串是否包含子串
-
- */
- function isContains(str, substr) {
- var strUp = str.toUpperCase();
- var substrUp = substr.toUpperCase()
- return
- new RegExp(substrUp).test(strUp);
- }
- /**
-
- * 去除字符串中特定的字符
-
- */
- function removeBytes(str, substr) {
- var items = str.split(substr)
- // 会得到一个数组,数组中包括利用o分割后的多个字符串(不包括o)
- var newStr = items.join("");
- return newStr
- // }
- }
- module.exports = {
- hexString2ArrayBuffer: hexString2ArrayBuffer,
- arrayBuffer2HexString: arrayBuffer2HexString,
- // getCRCCmd: getCRCCmd,
- getBCDTime: getBCDTime,
- stringToHex: stringToHex,
- stringXor: stringXor,
- getSecretEncoding: getSecretEncoding,
- hexToString: hexToString,
- getRamNumber: getRamNumber,
- isContains: isContains,
- removeBytes: removeBytes
- }
|