Foro de elhacker.net

Programación => Programación General => Mensaje iniciado por: sistemx en 19 Agosto 2012, 05:57 am



Título: Como convertir a UTF-8? Que es UTF-8?
Publicado por: sistemx en 19 Agosto 2012, 05:57 am
Hola! hmm me preguntaba acerca de que en realidad es utf-8 se que es como las paginas estan 'codificadas'. En fin el articulo de wikipedia me dejo confuso y con muchas dudas, encontre una pagina que 'convertia a utf-8' y puse a buscar la funcion en su codigo. Finalmente encontre una funcion de javascript que aparentemente lo hace..

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!