Skip to content

Instantly share code, notes, and snippets.

@taterbase
Created May 24, 2012 23:38
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save taterbase/2784890 to your computer and use it in GitHub Desktop.
Save taterbase/2784890 to your computer and use it in GitHub Desktop.
Convert bytes to string Javascript
function bin2string(array){
var result = "";
for(var i = 0; i < array.length; ++i){
result+= (String.fromCharCode(array[i]));
}
return result;
}
@helicc16
Copy link

Code works great for me. Thank you very much

@bkdotcom
Copy link

Doesn't support multi-byte encodings
Ie the single utf8 character: bin2String([0xE2, 0x98, 0xB9 ]);

@sourcx
Copy link

sourcx commented Dec 9, 2017

No, it will only work for ASCII.

@borgeslt
Copy link

array.map(function(b) { 
    return String.fromCharCode(b);
 } ).join("");

or

array.map(b => String.fromCharCode(b)).join("");

@iMplode-nZ
Copy link

Thanks, that's much better.

@hakeemta
Copy link

function bin2string(array){
var result = "";
for(var i = 0; i < array.length; ++i){
result+= (String.fromCharCode(array[i]));
}
return result;
}

works. Thanks.

@Sipu12345
Copy link

These things are not working when I am trying to print other languages. So Please give a suggestion regarding this matter.

@GeorgesOatesLarsen
Copy link

String.fromCharCode(...array)

will also work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment