js 各种进制转换,转ascll码等。十六进制转十进制、十进制转十六进制、十六进制转ASCII码
十六进制转十进制
function hex2dec(hex) { 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(function (acc, c) { acc = 16 * acc + c; return acc; }, 0); }
十进制转十六进制
function dec2hex(val){ return ("0" + (Number(val).toString(16))).toUpperCase()
//return ("0" + (Number(val).toString(16))).slice(-2).toUpperCase()
}
十六进制转二进制(需要借用到hex_dec,也就是需要先转换成十进制再转成二进制)
function hex2bin(hex, len) { var res = hex_dec(hex).toString(2); return (Array(len).join(0) + res).slice(-len); }
二进制转十六进制(网上的一种办法)
function bin2hex(str) { let hex_array = [{ 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" }] let value = '' let list = [] if (str.length % 4 !== 0) { let a = "0000" let b = a.substring(0, 4 - str.length % 4) str = b.concat(str) } while (str.length > 4) { list.push(str.substring(0, 4)) str = str.substring(4); } list.push(str) for (let i = 0; i < list.length; i++) { for (let j = 0; j < hex_array.length; j++) { if (list[i] == hex_array[j].val) { value = value.concat(hex_array[j].key) break } } } return value }
十六进制转ArrayBuffer
https://www.cnblogs.com/gradolabs/p/4762134.html
https://www.cnblogs.com/dxl395/p/11277858.html
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
function hex2buf(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; }
ArrayBufer转十六进制
function buf2hex(buffer) { // buffer is an ArrayBuffer return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join(''); }
十六进制转ASCII码
function hex2ascll(hexCharCodeStr) { var trimedStr = hexCharCodeStr.trim(); //去掉字符串两端的空格
var trimedStr =(trimedStr).toString(); //转换成字符串,如果是整数会出错 var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr; var len = rawStr.length; if (len % 2 !== 0) { alert("存在非法字符!"); return ""; } var curCharCode; var resultStr = []; for (var i = 0; i < len; i = i + 2) { curCharCode = parseInt(rawStr.substr(i, 2), 16); resultStr.push(String.fromCharCode(curCharCode)); } return resultStr.join(""); }
字符串转byte
function string2byte( str ) { var ch, st, re = []; for (var i = 0; i < str.length; i++ ) { ch = str.charCodeAt(i); // get char st = []; // set up "stack" do { st.push( ch & 0xFF ); // push byte to stack ch = ch >> 8; // shift value down by 1 byte } while ( ch ); // add stack contents to result // done because chars have "wrong" endianness re = re.concat( st.reverse() ); } // return an array of bytes return re; }