Created
December 27, 2013 03:46
-
-
Save deoxxa/8142322 to your computer and use it in GitHub Desktop.
Decode UTF-8 bytes
This file contains hidden or 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
| 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