js 二进制转换为16进制数
<!DOCTYPE html> <html> <head> <title>远程监控</title> </head> <body> <script> function bin_to_hex(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=[] console.log(str) if(str.length%4!==0){ let a = "0000" let b=a.substring(0,4-str.length%4) str = b.concat(str) } console.log(str) while (str.length > 4) { list.push(str.substring(0, 4)) str = str.substring(4); } list.push(str) console.log(list) 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 } } } console.log(value) return value } bin_to_hex("11111111"); </script> </body> </html>