Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

dataTool.js 3.0KB

1
  1. function strToHexCharCode(e){if(""===e)return"";for(var r=[],t=0;t<e.length;t++)r.push(e.charCodeAt(t).toString(16));return r.join("").toUpperCase()}function bccCheck(e){for(var r="",t=0;t<e.length/2;t++)r^=parseInt(e.substring(2*t,2*t+2),16);return 1==r.length&&(r="0"+r),tenToHex(r,2).toUpperCase()}function tenToHex(e,r,t){for(var n=e.toString(16),o="",a=0;a<r-n.length;a++)o+="0";var l="";return t?(n.length%2==0||(n="0"+n,o=o.substring(1)),l=n+o):t||(l=o+n),l.toUpperCase()}function hexToString(e){var r,t=e.trim(),n="0x"===t.substr(0,2).toLowerCase()?t.substr(2):t,o=n.length;if(o%2!=0)return alert("Illegal Format ASCII Code!"),"";for(var a=[],l=0;l<o;l+=2)r=parseInt(n.substr(l,2),16),a.push(String.fromCharCode(r));return a.join("").toUpperCase()}function encodeUtf8(e){const r=encodeURIComponent(e),t=[];for(var n=0;n<r.length;n++){const e=r.charAt(n);if("%"===e){const e=r.charAt(n+1)+r.charAt(n+2),o=parseInt(e,16);t.push(o),n+=2}else t.push(e.charCodeAt(0))}return t}function hexToTen(e){return parseInt(e,16)}function str2utf8(e){for(var r,t="",n=0;n<e.length;n++)if((r=e.charCodeAt(n))<128)t+=e.charAt(n);else if(r<2048)t+=String.fromCharCode(192|r>>6&31),t+=String.fromCharCode(128|r>>0&63);else{if(!(r<65536))throw"不是UCS-2字符集";t+=String.fromCharCode(224|r>>12&15),t+=String.fromCharCode(128|r>>6&63),t+=String.fromCharCode(128|r>>0&63)}return t}function hexTobin(e){e=e.toUpperCase();let r=[{key:0,val:"0000"},{key:1,val:"0001"},{key:2,val:"0010"},{key:3,val:"0011"},{key:4,val:"0100"},{key:5,val:"0101"},{key:6,val:"0110"},{key:7,val:"0111"},{key:8,val:"1000"},{key:9,val:"1001"},{key:"A",val:"1010"},{key:"B",val:"1011"},{key:"C",val:"1100"},{key:"D",val:"1101"},{key:"E",val:"1110"},{key:"F",val:"1111"}],t="";for(let n=0;n<e.length;n++)for(let o=0;o<r.length;o++)if(e.charAt(n)==r[o].key){t=t.concat(r[o].val);break}return t}function binTohex(e){let r=[{key:0,val:"0000"},{key:1,val:"0001"},{key:2,val:"0010"},{key:3,val:"0011"},{key:4,val:"0100"},{key:5,val:"0101"},{key:6,val:"0110"},{key:7,val:"0111"},{key:8,val:"1000"},{key:9,val:"1001"},{key:"a",val:"1010"},{key:"b",val:"1011"},{key:"c",val:"1100"},{key:"d",val:"1101"},{key:"e",val:"1110"},{key:"f",val:"1111"}],t="",n=[];if(e.length%4!=0){e="0000".substring(0,4-e.length%4).concat(e)}for(;e.length>4;)n.push(e.substring(0,4)),e=e.substring(4);n.push(e);for(let e=0;e<n.length;e++)for(let o=0;o<r.length;o++)if(n[e]==r[o].val){t=t.concat(r[o].key);break}return t}function strToBuffer(e){let r=new ArrayBuffer(e.length/2),t=new Uint8Array(r);for(var n=0;n<e.length/2;n++)t[n]="0x"+e.substring(2*n,2*n+2);return r}function bufferTohex(e){return Array.prototype.map.call(new Uint8Array(e),function(e){return("00"+e.toString(16)).slice(-2)}).join("")}function isEmpty(e){return 0==e.length||null==e||"undefined"==e}module.exports={encodeUtf8:encodeUtf8,tenToHex:tenToHex,hexToTen:hexToTen,bccCheck:bccCheck,hexToString:hexToString,strToHexCharCode:strToHexCharCode,str2utf8:str2utf8,hexTobin:hexTobin,binTohex:binTohex,isEmpty:isEmpty,strToBuffer:strToBuffer,bufferTohex:bufferTohex};