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;
}
@GeorgesOatesLarsen
Copy link

String.fromCharCode(...array)

will also work

@kLiHz
Copy link

kLiHz commented Apr 27, 2024

String.fromCharCode(...array)

will also work

Please notice that the spread syntax in function calls, probabaly has a length limit.

@kLiHz
Copy link

kLiHz commented Apr 27, 2024

Hi @bkdotcom or anyone who is interested in multi-byte encodings, the TextDecoder could be used.

const a = [0xE2, 0x98, 0xB9];
const u8a = new Uint8Array(a);

const utf8decoder = new TextDecoder('utf-8');
const decodedText = utf8decoder.decode(u8a);

console.log(decodedText);

See also on a TypeScript Playground.

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