function convertToBytes(str) {
var bytes = [];
for(var i = 0; i < str.length; ++i) {
var charCode = str.charCodeAt(i);
bytes = bytes.concat([charCode & 0xff, charCode / 256 >>> 0]);
}
return bytes;
}
And my slightly improved, ES6 compatible version:
function convertToBytes(str) {
var bytes = [];
Array.from(str).forEach((char) => {
var charCode = char.charCodeAt(char);
bytes = bytes.concat([charCode & 0xff, charCode / 256 >>> 0]);
});
return bytes;
}
All credit goes out to BrunoLM at StackOverflow.
No comments:
Post a Comment