Skip to content

Instantly share code, notes, and snippets.

@soldair
Created January 28, 2014 00:53
Show Gist options
  • Save soldair/8660477 to your computer and use it in GitHub Desktop.
Save soldair/8660477 to your computer and use it in GitHub Desktop.
utf8 js functions
var UTF8_MASK = [0x80,0xc0,0xe0,0xf0];
function utf8l(b){
if (!(b & 0x80)) {
return 1;
} else if ((b & 0xE0) === 0xC0){
return 2;
} else if ((b & 0xF0) === 0xE0) {
return 3;
} else if ((b & 0xF8) === 0xF0) {
return 4;
} else {
return 0;
}
}
// length,byte,character so far,number charater
function utf8c(l,b,c,n){
c |= (b&~UTF8_MASK[l-1])<<(n-1)*6;
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment