Skip to content

Instantly share code, notes, and snippets.

@baranovxyz
Created May 28, 2018 20:15
Show Gist options
  • Save baranovxyz/d416d8a65ee0cda1fad1f0d5c3a8b626 to your computer and use it in GitHub Desktop.
Save baranovxyz/d416d8a65ee0cda1fad1f0d5c3a8b626 to your computer and use it in GitHub Desktop.
function utf8ToBytes (str) {
// TODO(user): Use native implementations if/when available
var p = 0
var buf = new ArrayBuffer(str.length * 1.5) // fixme Use better algo?
var out = new Uint8Array(buf)
for (var i = 0; i < str.length; i++) {
var c = str.charCodeAt(i)
if (c < 128) {
out[p++] = c
} else if (c < 2048) {
out[p++] = (c >> 6) | 192
out[p++] = (c & 63) | 128
} else if (
((c & 0xFC00) == 0xD800) && (i + 1) < str.length &&
((str.charCodeAt(i + 1) & 0xFC00) == 0xDC00)) {
// Surrogate Pair
c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) & 0x03FF)
out[p++] = (c >> 18) | 240
out[p++] = ((c >> 12) & 63) | 128
out[p++] = ((c >> 6) & 63) | 128
out[p++] = (c & 63) | 128
} else {
out[p++] = (c >> 12) | 224
out[p++] = ((c >> 6) & 63) | 128
out[p++] = (c & 63) | 128
}
}
return out.slice(0, p)
}
@baranovxyz
Copy link
Author

Much fastest way is actually to use TextEncoder and TextDecoder apis:
https://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers

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