Skip to content

Instantly share code, notes, and snippets.

@deoxxa
Created December 27, 2013 03:46
Show Gist options
  • Select an option

  • Save deoxxa/8142322 to your computer and use it in GitHub Desktop.

Select an option

Save deoxxa/8142322 to your computer and use it in GitHub Desktop.
Decode UTF-8 bytes
var decode = function decode(bytes) {
var points = [];
var i = 0, j = 0;
while (i < bytes.length) {
if ((bytes[i] & 0x80) === 0) {
points.push(bytes[i] & 0x7f);
i += 1;
} else if ((bytes[i] & 0xE0) === 0xC0) {
points.push(((bytes[i] & 0x1F) << 6) + (bytes[i+1] & 0x7f));
i += 2;
} else if ((bytes[i] & 0xF0) === 0xE0) {
points.push(((bytes[i] & 0x1F) << 12) + ((bytes[i+1] & 0x7f) << 6) + (bytes[i+2] & 0x7f));
i += 3;
} else if ((bytes[i] & 0xF8) === 0xF0) {
points.push(((bytes[i] & 0x1F) << 18) + ((bytes[i+1] & 0x7f) << 12) + ((bytes[i+2] & 0x7f) << 6) + (bytes[i+3] & 0x7f));
i += 4;
}
}
return points;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment