«

javascript将二进制转为字符串

老王 发布于 阅读:12009 Javascript


 

用js将二进制转为字符串,代码如下:

var a = "00110000001100000011011";
var array = new Array();
for(var i=0;i<a.length;i=i+8){
    var num = parseInt(a.substr(i,8),2);
    array.push(num);
}
console.log(array.toString());

var str = "";
for(var x=0;x<array.length;x++){
    var y = String.fromCharCode(array[x]);
    str += y;
}
console.log(str);
var str2 = "";
for(var j=0;j<str.length;j=j+4){
   console.log(str.substr(j,4));
   str2 += String.fromCharCode(parseInt(str.substr(j,4),16));
}
console.log(str2);

javascript 前端开发