|
|
- function formatTime(date) {
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- const hour = date.getHours()
- const minute = date.getMinutes()
- const second = date.getSeconds()
-
- return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
- }
-
- function formatNumber(n) {
- n = n.toString()
- return n[1] ? n : '0' + n
- }
-
-
- function uint8ArrayToString(fileData){
- let dataString = "";
- for (let i = 0; i < fileData.length; i++) {
- dataString += String.fromCharCode(fileData[i]);
- }
- return dataString
- }
-
- function stringToUint8Array(str){
- let arr = [];
- for (let i = 0, j = str.length; i < j; ++i) {
- arr.push(str.charCodeAt(i));
- }
- let tmpUint8Array = new Uint8Array(arr);
- return tmpUint8Array
- }
-
- function stringToUint8ArrayWithPadding(str, length, padding){
- let arr = [];
- for (let i = 0, j = str.length; i < j; ++i) {
- arr.push(str.charCodeAt(i));
- }
-
- for (let i = str.length; i < length; i++) {
- arr.push(padding);
- }
-
- let tmpUint8Array = new Uint8Array(arr);
- return tmpUint8Array
- }
-
-
-
-
- // 微信官方给的ArrayBuffer转十六进制示例
- function ab2hex(buffer) {
- let hexArr = Array.prototype.map.call(
- new Uint8Array(buffer),
- function (bit) {
- return ('00' + bit.toString(16)).slice(-2)
- }
- )
- return hexArr.join(',');
- }
-
- //转成可展会的文字
- function hexCharCodeToStr(hexCharCodeStr) {
- let trimedStr = hexCharCodeStr.trim();
- let rawStr = trimedStr.substr(0, 2).toLowerCase() === '0x' ? trimedStr.substr(2) : trimedStr;
- let len = rawStr.length;
- let curCharCode;
- let resultStr = [];
- for (let i = 0; i < len; i = i + 2) {
- curCharCode = parseInt(rawStr.substr(i, 2), 16);
- resultStr.push(String.fromCharCode(curCharCode));
- }
- return resultStr.join('');
- }
-
- //转换成需要的格式
- function buf2string(buffer) {
- let arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
-
- return arr.map((char, i) => {
- return String.fromCharCode(char);
- }).join('');
- }
-
- function decodeHexNibble(c) {
- if (c >= 48 && c <= 57) {
- return c - 48;
- } else if (c >= 65 && c <= 70) {
- return c - 65 + 10;
- } else {
- return c >= 97 && c <= 102 ? c - 97 + 10 : -1;
- }
- }
-
- function decodeHexByte(s, pos) {
- let hi = decodeHexNibble(s.charCodeAt(pos));
- let lo = decodeHexNibble(s.charCodeAt(pos + 1));
- return ((hi << 4) + lo);
- }
-
- function hexStringToBytesWithPadding(str, byteLen, padding) {
- let len = str.length
- let length = 2 * byteLen
-
- if (length >= len && (length & 1) == 0) {
- if (length == 0) {
- return [0];
- } else {
- let bytes = new Uint8Array(length >>> 1);
-
- for(let i = 0; i < len; i += 2) {
- bytes[i >>> 1] = decodeHexByte(str, i);
- }
-
- for(let i = 0; i < byteLen-len/2; i++)
- bytes[len/2+i] = padding;
-
- return bytes;
- }
- }
- else
- return [0];
- }
-
-
- function timeStringToBytes(timeStr, timeLen) {
- let s = timeStr;
- if(s.length < timeLen)
- {
- let tmp = timeLen-s.length
- for(let i = 0; i< tmp; i++)
- s = "0" + s;
- }
- else
- s = timeStr.substr(s.length-timeLen, s.length);
-
- let start = 0
- let length = s.length
-
- if (length >= 0 && (length & 1) == 0) {
- if (length == 0) {
- return new byte[0];
- } else {
- let bytes = new Uint8Array(length >>> 1);
-
- for(let i = 0; i < length; i += 2) {
- bytes[i >>> 1] = decodeHexByte(s, start + i);
- }
-
- return bytes;
- }
- }
- else
- return [0];
- }
-
- function time40ToBytes(dt) {
-
- var cs = dt.substring(0,4) + "/" + dt.substring(4,6) + "/" + dt.substring(6,8) + " " + dt.substring(8,10) + ":" + dt.substring(10,12)
- + ":" + dt.substring(12,14) + ":100";
-
- var milliTs = new Date(cs).getTime();
- var secondTs = milliTs/1000
- let ts = secondTs.toString(16)
- let t = timeStringToBytes(ts, 10)
- return t;
- }
-
- module.exports = {
- formatTime: formatTime,
- uint8ArrayToString: uint8ArrayToString,
- stringToUint8Array: stringToUint8Array,
- stringToUint8ArrayWithPadding: stringToUint8ArrayWithPadding,
- ab2hex: ab2hex,
- hexCharCodeToStr,
- hexCharCodeToStr: buf2string,
- decodeHexNibble,
- decodeHexNibble: decodeHexByte,
- hexStringToBytesWithPadding: hexStringToBytesWithPadding,
- timeStringToBytes: timeStringToBytes,
- time40ToBytes: time40ToBytes
- }
|