1 // 十进制转二进制 2 function binary (num, Bits) { 3 var d = parseInt(num / 2) 4 var resArry = [num % 2] 5 for(; d != 0; ) { 6 d = parseInt(d / 2) 7 resArry.push(d % 2) 8 } 9 if (Bits) { 10 for(var r = resArry.length; r < Bits; r++) { 11 resArry.unshift(0) 12 } 13 } 14 return resArry.join().replace(/,/g, '') 15 } 16 console.log(binary(348, 16))
binary (需要转的数字, 需要转成的位数)
例如:
binary(348, 16)
将348转为16位的二进制数