-1
在Javascript中執行此操作的最簡單方法是什麼?目前我的代碼是一個巨大的開關塊,有沒有更簡單的方法?在最短的代碼中將二進制轉換爲十六進制
當前代碼:
function convertBintoHex(input){
input = ""+input;
while(input.length < 8){
input = "0" + input;
}
input = [input.substring(0,4),input.substring(4,8)];
var output = "";
for(var i in input){
switch(input[i]){
case "0000":
output += 0;
break;
case "0001":
output += 1;
break;
case "0010":
output += 2;
break;
case "0011":
output += 3;
break;
case "0100":
output += 4;
break;
case "0101":
output += 5;
break;
case "0110":
output += 6;
break;
case "0111":
output += 7;
break;
case "1000":
output += 8;
break;
case "1001":
output += 9;
break;
case "1010":
output += 'A';
break;
case "1011":
output += 'B';
break;
case "1100":
output += 'C';
break;
case '1101':
output += 'D';
break;
case '1110':
output += 'E';
break;
case '1111':
output += 'F';
break;
}
}
while(output.charAt(0) == 0 && output.length > 1){
output = output.substring(1);
}
return "0x" + output;
}
確實[*此*](http://stackoverflow.com/questions/17204912/javascript-need-functions -to-convert-a-string-containing-binary-to-hex-then-co)或[* this *](http://stackoverflow.com/questions/7695450/how-to-program-hex2bin-in- javascript)的幫助? [** This **](https://gist.github.com/faisalman/4213592)是我個人的最愛! –
我搜索了這20次使用一堆不同的搜索,找不到這樣的事情。我的天啊。 – PitaJ