Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
function toByteArray(str) {
    const bytes = [];
    for (let i = 0; i < str.length; i++) {
      let charcode = str.charCodeAt(i);
      if (charcode < 0x80) {
        bytes.push(charcode);
      } else if (charcode < 0x800) {
        bytes.push(0xc0 | (charcode >> 6), 
                   0x80 | (charcode & 0x3f));
      } else if (charcode < 0xd800 || charcode >= 0xe000) {
        bytes.push(0xe0 | (charcode >> 12), 
                   0x80 | ((charcode >> 6) & 0x3f), 
                   0x80 | (charcode & 0x3f));
      } else {
        i++;
        charcode = 0x10000 + (((charcode & 0x3ff) << 10) |
                   (str.charCodeAt(i) & 0x3ff));
        bytes.push(0xf0 | (charcode >> 18), 
                   0x80 | ((charcode >> 12) & 0x3f), 
                   0x80 | ((charcode >> 6) & 0x3f), 
                   0x80 | (charcode & 0x3f));
      }
    }
    return bytes;
  }
function getBytes(str) {
  var utf8 = decodeURIComponent(str);
  var arr = [];
  for (var i = 0; i < utf8.length; i++) {
    arr.push(utf8.charCodeAt(i));
  }
  return arr;
};
console.log(getBytes('abcd§'));
console.log(toByteArray('abcd§'));
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers