Skip to content

Instantly share code, notes, and snippets.

@nlf
Created November 1, 2012 01:32
Show Gist options
  • Save nlf/3991059 to your computer and use it in GitHub Desktop.
Save nlf/3991059 to your computer and use it in GitHub Desktop.
read string from buffer
function reads(buf, start, end) {
var pos = start,
bytes = [],
byte,
byte2;
while (pos < end) {
byte = buf[pos];
if (byte > 191 && byte < 224) {
pos++;
byte = ((byte & 31) << 6) | (buf[pos] & 63);
} else if ((byte > 128 && byte < 191) || byte > 224) {
pos++;
byte2 = buf[pos];
pos++;
byte = ((byte & 15) << 12) | ((byte2 & 63) << 6) | (buf[pos] & 63);
}
bytes.push(byte);
pos++;
}
return String.fromCharCode.apply(String, bytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment