Skip to content

Instantly share code, notes, and snippets.

@seveves
Created February 7, 2018 15:24
Show Gist options
  • Save seveves/db021e66fc419812e7ee285b314bc6db to your computer and use it in GitHub Desktop.
Save seveves/db021e66fc419812e7ee285b314bc6db to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/gafodumawa
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
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 = unescape(encodeURIComponent(str));
return Array.from({ length: utf8.length }).map((_, i) => utf8.charCodeAt(i));
};
console.log(getBytes('abcd§ß'));
console.log(toByteArray('abcd§ß'));
</script>
<script id="jsbin-source-javascript" type="text/javascript">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 = unescape(encodeURIComponent(str));
return Array.from({ length: utf8.length }).map((_, i) => utf8.charCodeAt(i));
};
console.log(getBytes('abcd§ß'));
console.log(toByteArray('abcd§ß'));</script></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 = unescape(encodeURIComponent(str));
return Array.from({ length: utf8.length }).map((_, i) => utf8.charCodeAt(i));
};
console.log(getBytes('abcd§ß'));
console.log(toByteArray('abcd§ß'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment