Created
May 28, 2018 20:15
-
-
Save baranovxyz/d416d8a65ee0cda1fad1f0d5c3a8b626 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
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
https://weblog.rogueamoeba.com/2017/02/27/javascript-correctly-converting-a-byte-array-to-a-utf-8-string/
backward