Código:
function str2rstr_utf8(c) {//Transforms a string to utf_8. Apparently
var b = "";
var d = -1;
var a, e;
while (++d < c.length) {
a = c.charCodeAt(d);
e = d + 1 < c.length ? c.charCodeAt(d + 1) : 0;
if (0xd800 <= a && a <= 0xdbff && 0xdc00 >= e && e <= 0xdfff) {//finds High and low surrogates.
a = 0x10000 + ((a & 0x3ff) << 10) + (e & 0x3ff); //Extracts the last 10 digits moves them 10 bits to the right,
d++ //does the same with the other value, and adds 0x10000.
}
if (a <= 0x7f) {//any value below 0x7f will be left as it is.
b += String.fromCharCode(a)
} else if (a <= 0x7ff) {
b += String.fromCharCode(0xc0 | ((a >>> 6) & 0x1f), 0x80 | (a & 0x3f))
} else if (a <= 0xffff) {
b += String.fromCharCode(0xe0 | ((a >>> 12) & 0xf), 0x80 | ((a >>> 6) & 0x3f), 0x80 | (a & 0x3f))
} else if (a <= 0x1fffff) {
b += String.fromCharCode(0xf0 | ((a >>> 18) & 7), 0x80 | ((a >>> 12) & 0x3f), 0x80 | ((a >>> 6) & 0x3f), 0x80 | (a & 0x3f))
}
}
return b
}
Puedo entender lo que hace pero no exactamente lo que hace... los comentarios fueron añadidos. Me podrian explicar mejor como utf-8 funciona? y exactamente que hace la funcion, muchas gracias!