-
-
Save taterbase/2784890 to your computer and use it in GitHub Desktop.
function bin2string(array){ | |
var result = ""; | |
for(var i = 0; i < array.length; ++i){ | |
result+= (String.fromCharCode(array[i])); | |
} | |
return result; | |
} |
Doesn't support multi-byte encodings
Ie the single utf8 character: bin2String([0xE2, 0x98, 0xB9 ]);
No, it will only work for ASCII.
array.map(function(b) {
return String.fromCharCode(b);
} ).join("");
or
array.map(b => String.fromCharCode(b)).join("");
Thanks, that's much better.
function bin2string(array){
var result = "";
for(var i = 0; i < array.length; ++i){
result+= (String.fromCharCode(array[i]));
}
return result;
}
works. Thanks.
These things are not working when I am trying to print other languages. So Please give a suggestion regarding this matter.
String.fromCharCode(...array)
will also work
String.fromCharCode(...array)
will also work
Please notice that the spread syntax in function calls, probabaly has a length limit.
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.
Code works great for me. Thank you very much